ClientScriptManager.GetWebResourceUrl(Type, String) 方法

定义

获取对程序集内资源的 URL 引用。

public:
 System::String ^ GetWebResourceUrl(Type ^ type, System::String ^ resourceName);
public string GetWebResourceUrl (Type type, string resourceName);
member this.GetWebResourceUrl : Type * string -> string
Public Function GetWebResourceUrl (type As Type, resourceName As String) As String

参数

type
Type

资源类型。

resourceName
String

程序集中资源的完全限定名称。

返回

对资源的 URL 引用。

例外

Web 资源类型为 null

- 或 -

Web 资源名称为 null

- 或 -

Web 资源名称长度为零。

示例

下面的代码示例演示如何使用 GetWebResourceUrl 方法。 此示例中的 type 参数设置为包含资源的程序集中的类的类型。 参数 resourceName 是使用资源的完全限定路径(包括默认命名空间)指定的。

<%@ Page Language="C#"%>
<%@ Import Namespace="Samples.AspNet.CS.Controls" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
 
  public void Page_Load(Object sender, EventArgs e)
  {
    // Define the resource name and type.
    String rsname = "Samples.AspNet.CS.Controls.script_include.js";
    Type rstype = typeof(ClientScriptResourceLabel);
        
    // Get a ClientScriptManager reference from the Page class.
    ClientScriptManager cs = Page.ClientScript;

    // Write out the web resource url.
    ResourcePath.InnerHtml = cs.GetWebResourceUrl(rstype, rsname);

    // Register the client resource with the page.
    cs.RegisterClientScriptResource(rstype, rsname);

  }
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
  <head>
    <title>ClientScriptManager Example</title>
  </head>
  <body>
     <form    id="Form1"
            runat="server">
     The web resource path is 
     <span  id="ResourcePath"
            runat="server"/>.
     <br />
     <br />
     <input type="text" 
            id="Message" />     
     <input type="button" 
            onclick="DoClick()" 
            value="ClientClick" />
     </form>
  </body>
</html>
<%@ Page Language="VB" %>
<%@ Import Namespace="Samples.AspNet.VB.Controls" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)

    ' Define the resource name and type.
    Dim rsname As String = "Samples.AspNet.VB.Controls.script_include.js"
    Dim rstype As Type = GetType(ClientScriptResourceLabel)
    
    ' Get a ClientScriptManager reference from the Page class.
    Dim cs As ClientScriptManager = Page.ClientScript
    
    ' Write out the web resource url.
    ResourcePath.InnerHtml = cs.GetWebResourceUrl(rstype, rsname)
    
    ' Register the client resource with the page.
    cs.RegisterClientScriptResource(rstype, rsname)
    
  End Sub
  
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head>
    <title>ClientScriptManager Example</title>
  </head>
  <body>
     <form    id="Form1"
            runat="server">
     The web resource path is 
     <span  id="ResourcePath"
            runat="server"/>.
     <br />
     <br />
     <input type="text" 
            id="Message" />     
     <input type="button" 
            onclick="DoClick()" 
            value="ClientClick" />
     </form>
  </body>
</html>

下面的代码示例演示如何以编程方式应用 WebResourceAttribute 元数据属性,以标记将要提供的资源的程序集。 使用默认命名空间设置为 Samples.AspNet.CS.ControlsSamples.AspNet.VB.Controls的类库中编译以下类,具体取决于所使用的语言。

using System;
using System.Web;
using System.Web.UI;
using System.Security.Permissions;

[assembly: WebResource("Samples.AspNet.CS.Controls.script_include.js", "application/x-javascript")]
namespace Samples.AspNet.CS.Controls
{
    [AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal)]
    public class ClientScriptResourceLabel
    {
        // Class code goes here.
    }
}
Imports System.Web
Imports System.Web.UI
Imports System.Security.Permissions

<Assembly: WebResource("Samples.AspNet.VB.Controls.script_include.js", "application/x-javascript")> 
Namespace Samples.AspNet.VB.Controls

    <AspNetHostingPermission(SecurityAction.Demand, Level:=AspNetHostingPermissionLevel.Minimal)> _
    Public Class ClientScriptResourceLabel

        ' Class code goes here.

    End Class

End Namespace

此示例需要名为 的 Script_include.jsJavaScript 文件。 .js 文件是包含 ClientScriptResourceLabel 对象的程序集中的嵌入资源。 如果使用 Visual Studio,在类库项目的属性窗口中,选择脚本文件时,将“生成操作”设置为“嵌入资源”。 如果要在命令行上编译库,请使用 /resource 开关嵌入资源。

function DoClick() {Form1.Message.value='Text from resource script.'}  

注解

方法 GetWebResourceUrl 返回对程序集中嵌入的资源的 URL 引用。 返回的引用未经过 URL 编码。 资源可以是脚本文件、图像或任何静态文件。 根据将访问资源的对象指定类型。

向页面注册的 Web 资源通过其类型和名称进行唯一标识。 只有一个具有给定类型和名称对的资源可以注册到页面。 尝试注册已注册的资源不会创建已注册资源的副本。

方法 GetWebResourceUrl 与 方法结合使用, RegisterClientScriptResource 用于访问程序集中嵌入的资源。 有关在应用程序中使用资源的详细信息,请参阅 ASP.NET 网页资源概述

适用于

另请参阅