ClientScriptManager GetWebResourceUrl 方法
更新:2007 年 11 月
来源:http://msdn.microsoft.com/zh-cn/library/system.web.ui.clientscriptmanager.getwebresourceurl.aspx
http://msdn.microsoft.com/zh-cn/library/system.web.ui.page.clientscript(VS.80).aspx
获取对程序集内资源的 URL 引用。
命名空间: System.Web.UI
程序集: System.Web(在 System.Web.dll 中)

Public Function GetWebResourceUrl ( _ type As Type, _ resourceName As String _ ) As String
Dim instance As ClientScriptManager Dim type As Type Dim resourceName As String Dim returnValue As String returnValue = instance.GetWebResourceUrl(type, _ resourceName)
public string GetWebResourceUrl( Type type, string resourceName )
public:
String^ GetWebResourceUrl(
Type^ type,
String^ resourceName
)
public String GetWebResourceUrl(
Type type,
String resourceName
)
public function GetWebResourceUrl( type : Type, resourceName : String ) : String

异常 | 条件 |
---|---|
ArgumentNullException |
Web 资源类型为 nullNothingnullptrnull 引用(在 Visual Basic 中为 Nothing)。 |
ArgumentNullException |
Web 资源名称为 nullNothingnullptrnull 引用(在 Visual Basic 中为 Nothing)。 - 或 - Web 资源名称长度为零。 |

GetWebResourceUrl 方法返回对程序集内嵌入的资源的 URL 引用。返回的引用不是 URL 编码的。资源可以是脚本文件、图像或任何静态文件。根据将要访问资源的对象来指定类型。
使用该页面注册的 Web 资源由其类型和名称唯一标识。只有一个具有给定的类型和名称对的资源可向该页面注册。试图注册一个已经注册的资源不会创建已注册资源的副本。
GetWebResourceUrl 方法与 RegisterClientScriptResource 方法结合使用以访问嵌入程序集中的资源。有关在应用程序中使用资源的更多信息,请参见 ASP.NET 网页资源概述。

下面的代码示例演示 GetWebResourceUrl 方法的用法。此示例中的 type 参数设置为包含此资源的程序集中的类的类型。用包含默认命名空间的资源的完全限定路径指定 resourceName 参数。
<%@ 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 > <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="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 > <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.Controls 或 Samples.AspNet.VB.Controls(具体取决于所使用的语言)的默认命名空间编译类库中的以下类。
Imports Microsoft.VisualBasic Imports System 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
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. } }
此示例需要一个名为 Script_include.js 的 JavaScript 文件。该 .js 文件是程序集内包含 ClientScriptResourceLabel 对象的嵌入资源。如果要使用 Visual Studio,请在选择脚本文件时在类库项目的“属性”窗口中将“生成操作”设置为“嵌入资源”。如果要在命令行编译库,请使用 /resource 开关嵌入资源。
注意:此属性在 .NET Framework 2.0 版中是新增的。
获取用于管理脚本、注册脚本和向页添加脚本的 ClientScriptManager 对象。
命名空间:System.Web.UI
程序集:System.Web(在 system.web.dll 中)

Public ReadOnly Property ClientScript As ClientScriptManager
Dim instance As Page Dim value As ClientScriptManager value = instance.ClientScript
public ClientScriptManager ClientScript { get; }
public: property ClientScriptManager^ ClientScript { ClientScriptManager^ get (); }
/** @property */ public ClientScriptManager get_ClientScript ()
public function get ClientScript () : ClientScriptManager
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述