ClientScriptManager.GetWebResourceUrl(Type, String) Metodo

Definizione

Ottiene un riferimento URL a una risorsa in un assembly.

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

Parametri

type
Type

Tipo di risorsa.

resourceName
String

Nome completo della risorsa nell'assembly.

Restituisce

String

Riferimento URL alla risorsa.

Eccezioni

Il tipo di risorsa Web è null.

-oppure-

Il nome della risorsa Web è null.

-oppure- La lunghezza del nome della risorsa Web è pari a zero.

Esempio

Nell'esempio di codice seguente viene illustrato l'uso GetWebResourceUrl del metodo. Il parametro di tipo in questo esempio è impostato sul tipo di classe nell'assembly contenente la risorsa. Il resourceName parametro viene specificato con il percorso completo della risorsa, che include lo spazio dei nomi predefinito.

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

Nell'esempio di codice seguente viene illustrato come applicare a livello di codice l'attributo dei metadati per contrassegnare l'assembly WebResourceAttribute per le risorse che verranno gestite. Compilare la classe seguente in una libreria di classi con uno spazio dei nomi predefinito impostato su Samples.AspNet.CS.Controls o Samples.AspNet.VB.Controls, a seconda della lingua in uso.

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

In questo esempio è necessario un file JavaScript denominato Script_include.js. Il file .js è una risorsa incorporata nell'assembly che contiene l'oggetto ClientScriptResourceLabel . Se si usa Visual Studio, nella Finestra Proprietà del progetto della libreria di classi impostare Azione di compilazione su Risorsa incorporata quando viene selezionato il file di script. Se si compila la libreria nella riga di comando, usare l'opzione /resource per incorporare la risorsa.

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

Commenti

Il GetWebResourceUrl metodo restituisce un riferimento URL a una risorsa incorporata in un assembly. Il riferimento restituito non è codificato con URL. Le risorse possono essere file di script, immagini o qualsiasi file statico. Specificare il tipo in base all'oggetto che accederà alla risorsa.

Una risorsa Web registrata con la pagina è identificata in modo univoco dal relativo tipo e nome. È possibile registrare una sola risorsa con un tipo e una coppia di nomi specificata con la pagina. Il tentativo di registrare una risorsa già registrata non crea un duplicato della risorsa registrata.

Il GetWebResourceUrl metodo viene usato insieme al RegisterClientScriptResource metodo per accedere alle risorse incorporate negli assembly. Per altre informazioni sull'uso delle risorse nelle applicazioni, vedere ASP.NET Panoramica delle risorse della pagina Web.

Si applica a

Vedi anche