ClientScriptManager.GetWebResourceUrl(Type, String) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
會取得一個組合語言中資源的 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
集合中資源的完全限定名稱。
傳回
資源的網址參考。
例外狀況
範例
以下程式碼範例示範了此 GetWebResourceUrl 方法的使用。 此範例中的 型態 參數設為包含該資源的組合語言類別的型別。
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.Controls 或 Samples.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.js. 的 JavaScript 檔案。 .js 檔案是裝配中嵌入的資源,包含該 ClientScriptResourceLabel 物件。 如果你使用 Visual Studio,在類別函式庫專案的屬性視窗中,選取腳本檔案時,將 Build Action 設為 Embedded Resource。 如果你是在命令列編譯函式庫,請使用 /resource switch 來嵌入資源。
function DoClick() {Form1.Message.value='Text from resource script.'}
備註
該 GetWebResourceUrl 方法回傳一個嵌入組合語言中的資源的 URL 參考。 回傳的參考文獻並未編碼 URL。 資源可以是腳本檔案、圖片或任何靜態檔案。 你可以根據將要存取資源的物件來指定類型。
與該頁面註冊的網頁資源會以其類型和名稱唯一識別。 該頁面只能註冊一個具有特定類型與名稱配對的資源。 嘗試註冊已註冊的資源不會產生該資源的重複。
此 GetWebResourceUrl 方法與存取嵌入組件中資源的方法同時 RegisterClientScriptResource 使用。 欲了解更多關於在應用程式中使用資源的資訊,請參閱 ASP.NET 網頁資源概覽。