WebResourceAttribute 類別

定義

定義中繼資料屬性,以啟用組件中的內嵌資源。 此類別無法獲得繼承。

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
繼承
WebResourceAttribute
屬性

範例

本節包含兩個程式碼範例。 第一個程式碼範例示範如何將 屬性套用 WebResourceAttribute 至定義自訂控制項的命名空間。 MyCustomControl 第二個程式碼範例示範如何在網頁中使用 MyCustomControl 類別。

下列程式碼範例示範如何在自訂群組件上套用 WebResourceAttribute 屬性,以定義影像 Web 資源和 HTML Web 資源。 類別 MyCustomControl 會定義複合控制項,該控制項會使用資源來設定包含在複合控制項內的控制項屬性值 ImageUrl Image ,以及設定 HRef 控制項連結至 HTML 資源的控制項屬性 HtmlAnchor

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

下列程式碼範例示範如何在網頁中使用 MyCustomControl 類別。

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

此範例會要求您使用包含 MyCustomControl 的元件編譯Image1.jpg和Help.htm資源。 如需詳細資訊,請參閱/resource (C# 編譯器選項) /resource (Visual Basic)

接下來會顯示可用於此範例的 HTML Web 資源範例。 請注意,當您 WebResource 將 Web 資源的 屬性 true 設定 PerformSubstitution 為 時,會使用 語法。

<!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>

備註

只有在元件宣告上使用時,類別 WebResourceAttribute 才有效。 它用來在元件中啟用指定的內嵌資源,以作為 Web 資源使用。

如需資源的詳細資訊,請參閱ASP.NET 網頁資源概觀

建構函式

WebResourceAttribute(String, String)

使用指定的 Web 資源和資源內容類型,初始化 WebResourceAttribute 類別的新執行個體。

屬性

CdnPath

取得或設定包含 Web 資源之內容提供網路 (CDN) 的路徑。

CdnSupportsSecureConnection

取得或設定值,這個值向 ScriptManager 表示當使用 HTTPS 存取頁面時,是否應該使用內容提供網路 (CDN) 路徑的安全連接存取指令碼資源。

ContentType

取得字串,包含 WebResourceAttribute 類別所參考之資源的 MIME 類型。

LoadSuccessExpression

取得或設定 Web 資源已成功載入時所使用的運算式。

PerformSubstitution

取得或設定布林值,判斷處理 WebResourceAttribute 類別所參考的內嵌資源期間,是否剖析其他 Web 資源 URL 並取代成資源的完整路徑。

TypeId

在衍生類別中實作時,取得這個 Attribute 的唯一識別碼。

(繼承來源 Attribute)
WebResource

取得字串,包含 WebResourceAttribute 類別所參考之資源的名稱。

方法

Equals(Object)

傳回值,這個值指出此執行個體是否與指定的物件相等。

(繼承來源 Attribute)
GetHashCode()

傳回這個執行個體的雜湊碼。

(繼承來源 Attribute)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
IsDefaultAttribute()

在衍生類別中覆寫時,表示這個執行個體的值是衍生類別的預設值。

(繼承來源 Attribute)
Match(Object)

在衍生類別中覆寫時,會傳回值,表示這個執行個體是否等於指定物件。

(繼承來源 Attribute)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

明確介面實作

_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。

(繼承來源 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

擷取物件的類型資訊,可以用來取得介面的類型資訊。

(繼承來源 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

擷取物件提供的類型資訊介面數目 (0 或 1)。

(繼承來源 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供物件所公開的屬性和方法的存取權。

(繼承來源 Attribute)

適用於

另請參閱