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 リソース名の長さがゼロです。

次のコード例は、 メソッドの使用方法を 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.VB.Controlsに設定してSamples.AspNet.CS.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

この例では、 という名前の JavaScript ファイルが必要です Script_include.js。 .js ファイルは、 オブジェクトを含む ClientScriptResourceLabel アセンブリ内の埋め込みリソースです。 Visual Studio を使用している場合は、クラス ライブラリ プロジェクトのプロパティ ウィンドウで、スクリプト ファイルが選択されているときに [ビルド アクション] を [埋め込みリソース] に設定します。 コマンド ラインでライブラリをコンパイルする場合は、 /resource スイッチを使用してリソースを埋め込みます。

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

注釈

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

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

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

適用対象

こちらもご覧ください