WebResourceAttribute Classe
Definizione
Importante
Alcune informazioni sono relative alla release non definitiva del prodotto, che potrebbe subire modifiche significative prima della release definitiva. Microsoft non riconosce alcuna garanzia, espressa o implicita, in merito alle informazioni qui fornite.
Definisce l'attributo di metadati che abilita una risorsa incorporata in un assembly. Questa classe non può essere ereditata.
public ref class WebResourceAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.Assembly, AllowMultiple=true)]
public sealed class WebResourceAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Assembly, AllowMultiple=true)>]
type WebResourceAttribute = class
inherit Attribute
Public NotInheritable Class WebResourceAttribute
Inherits Attribute
- Ereditarietà
- Attributi
Esempio
Questa sezione contiene due esempi di codice. Il primo esempio di codice illustra come applicare l'attributo WebResourceAttribute a uno spazio dei nomi che definisce un controllo personalizzato, MyCustomControl. Il secondo esempio di codice illustra come usare la MyCustomControl classe in una pagina Web.
Nell'esempio di codice seguente viene illustrato come applicare l'attributo WebResourceAttribute in un assembly personalizzato per definire una risorsa Web immagine e una risorsa Web HTML. La MyCustomControl classe definisce un controllo composito che utilizza le risorse per impostare il valore della ImageUrl proprietà di un Image controllo contenuto all'interno del controllo composito e per impostare la HRef proprietà di un HtmlAnchor controllo che collega alla risorsa HTML.
using System;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
[assembly: WebResource("image1.jpg", "image/jpeg")]
[assembly: WebResource("help.htm", "text/html", PerformSubstitution=true)]
namespace Samples.AspNet.CS.Controls
{
public class MyCustomControl : Control
{
[System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name="FullTrust")]
protected override void CreateChildControls()
{
// Create a new Image control.
Image _img = new Image();
_img.ImageUrl = this.Page.ClientScript.GetWebResourceUrl(typeof(MyCustomControl), "image1.jpg");
this.Controls.Add(_img);
// Create a new Label control.
Label _lab = new Label();
_lab.Text = "A composite control using the WebResourceAttribute class.";
this.Controls.Add(_lab);
// Create a new HtmlAnchor control linking to help.htm.
HtmlAnchor a = new HtmlAnchor();
a.HRef = this.Page.ClientScript.GetWebResourceUrl(typeof(MyCustomControl), "help.htm");
a.InnerText = "help link";
this.Controls.Add(new LiteralControl("<br />"));
this.Controls.Add(a);
}
}
}
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.HtmlControls
Imports System.Web.UI.WebControls
<Assembly: WebResource("image1.gif", "image/jpeg")>
<Assembly: WebResource("help.htm", "text/html", PerformSubstitution:=True)>
Namespace Samples.AspNet.VB.Controls
Public Class MyCustomControl
Inherits Control
<System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> _
Protected Overrides Sub CreateChildControls()
' Create a new Image control.
Dim _img As New Image()
_img.ImageUrl = Me.Page.ClientScript.GetWebResourceUrl(GetType(MyCustomControl), "image1.jpg")
Me.Controls.Add(_img)
' Create a new Label control.
Dim _lab As New Label()
_lab.Text = "A composite control using the WebResourceAttribute class."
Me.Controls.Add(_lab)
' Create a new HtmlAnchor control linking to help.htm.
Dim a As HtmlAnchor = New HtmlAnchor()
a.HRef = Me.Page.ClientScript.GetWebResourceUrl(GetType(MyCustomControl), "help.htm")
a.InnerText = "help link"
Me.Controls.Add(New LiteralControl("<br />"))
Me.Controls.Add(a)
End Sub
End Class
End Namespace
Nell'esempio di codice seguente viene illustrato come usare la MyCustomControl classe in una pagina Web.
<%@ Page Language="C#" %>
<%@ Register TagPrefix="AspNetSamples" Namespace="Samples.AspNet.CS.Controls" Assembly="Samples.AspNet.CS.Controls" %>
<%@ Import Namespace="System.Reflection" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
// Get the assembly metatdata.
Type clsType = typeof(MyCustomControl);
Assembly a = clsType.Assembly;
// Iterate through the attributes for the assembly.
foreach (Attribute attr in Attribute.GetCustomAttributes(a))
{
//Check for WebResource attributes.
if (attr.GetType() == typeof(WebResourceAttribute))
{
WebResourceAttribute wra = (WebResourceAttribute)attr;
Response.Write("Resource in the assembly: " + wra.WebResource.ToString() +
" with ContentType = " + wra.ContentType.ToString() +
" and PerformsSubstitution = " + wra.PerformSubstitution.ToString() + "</br>");
}
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>WebResourceAttribute Example</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<AspNetSamples:MyCustomControl id="MyCustomControl1" runat="server">
</AspNetSamples:MyCustomControl>
</div>
</form>
</body>
</html>
<%@ Page Language="VB" %>
<%@ Register TagPrefix="AspNetSamples" Namespace="Samples.AspNet.VB.Controls" Assembly="Samples.AspNet.VB.Controls" %>
<%@ Import Namespace="System.Reflection" %>
<!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)
' Get the assembly metatdata.
Dim clsType As Type = GetType(MyCustomControl)
Dim a As Assembly = clsType.Assembly
For Each attr As Attribute In Attribute.GetCustomAttributes(a)
'Check for WebResource attributes.
If attr.GetType() Is GetType(WebResourceAttribute) Then
Dim wra As WebResourceAttribute = CType(attr, WebResourceAttribute)
Response.Write("Resource in the assembly: " & wra.WebResource.ToString() & _
" with ContentType = " & wra.ContentType.ToString() & _
" and PerformsSubstitution = " & wra.PerformSubstitution.ToString() & "</br>")
End If
Next attr
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>WebResourceAttribute Example</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<AspNetSamples:MyCustomControl id="MyCustomControl1" runat="server">
</AspNetSamples:MyCustomControl>
</div>
</form>
</body>
</html>
In questo esempio è necessario compilare le risorse Image1.jpg e Help.htm con l'assembly che contiene MyCustomControl. Per altre informazioni, vedere /resource (opzioni del compilatore C#) o /resource (Visual Basic).For more information, see, /resource (C#Compiler Options) or /resource (Visual Basic).
Di seguito è riportato un esempio di risorsa Web HTML che può essere usata in questo esempio. Si noti l'uso della WebResource sintassi , che viene usata quando si imposta la PerformSubstitution proprietà su true per una risorsa Web.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html >
<head>
<title>Included Help Page</title>
</head>
<body>
<div>
<img alt="help image" src=<% = WebResource("image1.jpg") %> />
Included help file.
</div>
</body>
</html>
Commenti
La WebResourceAttribute classe è valida solo se utilizzata nelle dichiarazioni di assembly. Viene usato per abilitare una risorsa incorporata specificata in un assembly da usare come risorsa Web.
Per altre informazioni sulle risorse, vedere ASP.NET Panoramica delle risorse della pagina Web.
Costruttori
| Nome | Descrizione |
|---|---|
| WebResourceAttribute(String, String) |
Inizializza una nuova istanza della WebResourceAttribute classe con la risorsa Web e il tipo di contenuto della risorsa Web specificati. |
Proprietà
| Nome | Descrizione |
|---|---|
| CdnPath |
Ottiene o imposta il percorso di una rete per la distribuzione di contenuti che contiene risorse Web. |
| CdnSupportsSecureConnection |
Ottiene o imposta un valore che indica se è necessario accedere a ScriptManager una risorsa script usando una connessione sicura al percorso della rete per la distribuzione di contenuti (CDN) quando si accede alla pagina tramite HTTPS. |
| ContentType |
Ottiene una stringa contenente il tipo MIME della risorsa a cui fa riferimento la WebResourceAttribute classe . |
| LoadSuccessExpression |
Ottiene o imposta un'espressione utilizzata quando una risorsa Web è stata caricata correttamente. |
| PerformSubstitution |
Ottiene o imposta un valore booleano che determina se, durante l'elaborazione della risorsa incorporata a cui fa riferimento la WebResourceAttribute classe , altri URL di risorse Web vengono analizzati e sostituiti con il percorso completo della risorsa. |
| TypeId |
Se implementato in una classe derivata, ottiene un identificatore univoco per questo Attribute. (Ereditato da Attribute) |
| WebResource |
Ottiene una stringa contenente il nome della risorsa a cui fa riferimento la WebResourceAttribute classe . |
Metodi
| Nome | Descrizione |
|---|---|
| Equals(Object) |
Restituisce un valore che indica se questa istanza è uguale a un oggetto specificato. (Ereditato da Attribute) |
| GetHashCode() |
Restituisce il codice hash per questa istanza. (Ereditato da Attribute) |
| GetType() |
Ottiene il Type dell'istanza corrente. (Ereditato da Object) |
| IsDefaultAttribute() |
Quando sottoposto a override in una classe derivata, indica se il valore di questa istanza è il valore predefinito per la classe derivata. (Ereditato da Attribute) |
| Match(Object) |
Quando sottoposto a override in una classe derivata, restituisce un valore che indica se questa istanza è uguale a un oggetto specificato. (Ereditato da Attribute) |
| MemberwiseClone() |
Crea una copia superficiale del Objectcorrente. (Ereditato da Object) |
| ToString() |
Restituisce una stringa che rappresenta l'oggetto corrente. (Ereditato da Object) |
Implementazioni dell'interfaccia esplicita
| Nome | Descrizione |
|---|---|
| _Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr) |
Esegue il mapping di un set di nomi a un set corrispondente di ID dispatch. (Ereditato da Attribute) |
| _Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) |
Recupera le informazioni sul tipo per un oggetto, che può essere utilizzato per ottenere le informazioni sul tipo per un'interfaccia. (Ereditato da Attribute) |
| _Attribute.GetTypeInfoCount(UInt32) |
Recupera il numero delle interfacce di informazioni sul tipo fornite da un oggetto (0 o 1). (Ereditato da Attribute) |
| _Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) |
Fornisce l'accesso alle proprietà e ai metodi esposti da un oggetto . (Ereditato da Attribute) |