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

這個範例需要名為的 JavaScript 檔案Script_include.jsThis 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 Web Page Resources OverviewFor more information on using resources in applications, see ASP.NET Web Page Resources Overview.

適用於

另請參閱