ClientScriptManager.GetWebResourceUrl(Type, String) ClientScriptManager.GetWebResourceUrl(Type, String) ClientScriptManager.GetWebResourceUrl(Type, String) Method

定義

取得對組件中資源的 URL 參考。Gets a URL reference to a resource in an assembly.

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

參數

type
Type Type Type

資源的型別。The type of the resource.

resourceName
String String String

組件中資源的完整名稱。The fully qualified name of the resource in the assembly.

傳回

對資源的 URL 參考。The URL reference to the resource.

例外狀況

Web 資源類型為 nullThe web resource type is null.

-或--or-

Web 資源名稱為 nullThe web resource name is null.

-或--or- Web 資源名稱長度為零。The web resource name has a length of zero.

範例

下列程式碼範例示範如何使用GetWebResourceUrl方法。The following code example demonstrates the use of the GetWebResourceUrl method. 這個範例中的別參數會設定為包含資源之元件中的類別類型。The type parameter in this example is set to the type of class in the assembly containing the resource. resourceName參數是以資源的完整路徑指定, 其中包括預設命名空間。The resourceName parameter is specified with the fully qualified path to the resource, which includes the default namespace.

<%@ 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套用中繼資料屬性, 以標記將提供服務的資源元件。The following code example demonstrates how to programmatically apply the WebResourceAttribute metadata attribute to mark the assembly for the resources that will be served. 根據您使用的語言, 將預設命名空間設為Samples.AspNet.CS.ControlsSamples.AspNet.VB.Controls的類別庫中的下列類別編譯。Compile the following class in a class library with a default namespace set to Samples.AspNet.CS.Controls or Samples.AspNet.VB.Controls, depending on what language you are using.

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 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

這個範例需要名為Script_include.js的 JavaScript 檔案。This example requires a JavaScript file named Script_include.js. .Js 檔案是包含ClientScriptResourceLabel物件之元件中的內嵌資源。The .js file is an embedded resource in the assembly that contains the ClientScriptResourceLabel object. 如果您使用 Visual Studio, 請在類別庫專案的屬性視窗中, 將 [組建動作] 設為 內嵌資源If you are using Visual Studio, in the Properties window of the class library project, set Build Action to Embedded Resource when the script file is selected. 如果您要在命令列編譯程式庫, 請使用/resource參數來內嵌資源。If you are compiling the library at the command line, use the /resource switch to embed the resource.

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

備註

GetWebResourceUrl方法會將 URL 參考傳回給內嵌在元件中的資源。The GetWebResourceUrl method returns a URL reference to a resource embedded in an assembly. 傳回的參考不是 URL 編碼。The returned reference is not URL encoded. 資源可以是腳本檔案、影像或任何靜態檔案。Resources can be script files, images, or any static file. 您可以根據將要存取資源的物件來指定類型。You specify the type based on the object that will be accessing the resource.

向頁面註冊的 Web 資源是由其類型和名稱唯一識別。A Web resource registered with the page is uniquely identified by its type and name. 只有一個具有指定類型和名稱組的資源可以向頁面註冊。Only one resource with a given type and name pair can be registered with the page. 嘗試註冊已註冊的資源, 並不會建立已註冊資源的複本。Attempting to register a resource that is already registered does not create a duplicate of the registered resource.

GetWebResourceUrl方法會RegisterClientScriptResource與方法搭配使用, 以存取內嵌于元件中的資源。The GetWebResourceUrl method is used in conjunction with the RegisterClientScriptResource method for accessing resources embedded in assemblies. 如需在應用程式中使用資源的詳細資訊, 請參閱ASP.NET 網頁資源總覽For more information on using resources in applications, see ASP.NET Web Page Resources Overview.

適用於

另請參閱