共用方式為


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 屬性,以定義影像網路資源與 HTML 網頁資源。 該MyCustomControl類別定義了一個複合控制項,利用資源設定包含在該控制項內的Image控制項屬性的值ImageUrl,以及連結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 網頁資源範例。 請注意語法的使用 WebResource ,當你將屬性設定 PerformSubstitution 為 時 true ,會用到這個語法作為網頁資源。

<!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 類別僅在組合宣告中使用時有效。 它用於啟用組件中指定的嵌入式資源,作為網頁資源使用。

欲了解更多資源資訊,請參閱 ASP.NET 網頁資源概覽

建構函式

名稱 Description
WebResourceAttribute(String, String)

初始化一個新的類別實例 WebResourceAttribute ,使用指定的網頁資源與資源內容類型。

屬性

名稱 Description
CdnPath

取得或設定包含網路資源的內容傳遞網路(CDN)路徑。

CdnSupportsSecureConnection

取得或設定一個值,指示 ScriptManager 當頁面以 HTTPS 存取時,是否應該透過安全連線存取內容傳遞網路(CDN)路徑來存取腳本資源。

ContentType

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

LoadSuccessExpression

取得或設定一個表達式,當網路資源成功載入時會使用。

PerformSubstitution

取得或設定一個布林值,決定在處理類別所參考 WebResourceAttribute 的內嵌資源時,是否會解析其他網路資源的網址,並替換為該資源的完整路徑。

TypeId

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

(繼承來源 Attribute)
WebResource

會取得一個包含類別參考 WebResourceAttribute 資源名稱的字串。

方法

名稱 Description
Equals(Object)

傳回值,這個值表示這個實例是否等於指定的物件。

(繼承來源 Attribute)
GetHashCode()

傳回這個實例的哈希碼。

(繼承來源 Attribute)
GetType()

取得目前實例的 Type

(繼承來源 Object)
IsDefaultAttribute()

在衍生類別中覆寫時,指出這個實例的值是否為衍生類別的預設值。

(繼承來源 Attribute)
Match(Object)

在衍生類別中覆寫時,傳回值,指出這個實例是否等於指定的物件。

(繼承來源 Attribute)
MemberwiseClone()

建立目前 Object的淺層複本。

(繼承來源 Object)
ToString()

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

(繼承來源 Object)

明確介面實作

名稱 Description
_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)

適用於

另請參閱