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 リソース名の長さは 0 です。

次のコード例は、 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.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 ActionEmbedded Resource に設定します。 コマンド ラインでライブラリをコンパイルする場合は、 /resource スイッチを使用してリソースを埋め込みます。

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

注釈

GetWebResourceUrl メソッドは、アセンブリに埋め込まれたリソースへの URL 参照を返します。 返された参照は URL エンコードされていません。 リソースには、スクリプト ファイル、イメージ、または任意の静的ファイルを指定できます。 リソースにアクセスするオブジェクトに基づいて型を指定します。

ページに登録されている Web リソースは、その種類と名前によって一意に識別されます。 ページに登録できるのは、特定の種類と名前のペアを持つリソースが 1 つだけです。 既に登録されているリソースを登録しようとしても、登録済みリソースの重複は作成されません。

GetWebResourceUrl メソッドは、アセンブリに埋め込まれているリソースにアクセスするためのRegisterClientScriptResource メソッドと組み合わせて使用されます。 アプリケーションでリソースを使用する方法の詳細については、「ASP.NET Web ページ リソースの概要を参照してください。

適用対象

こちらもご覧ください