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类定义一个复合控件,该控件使用资源来设置包含在复合控件中的控件的属性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 Web 资源的示例。 请注意语法 WebResource 的使用,该语法在将属性设置为 PerformSubstitution true 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>

注解

仅当在程序集声明上使用时,该 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)

将一组名称映射为对应的一组调度标识符。

(继承自 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

检索对象的类型信息,然后可以使用该信息获取接口的类型信息。

(继承自 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

检索对象提供的类型信息接口的数量(0 或 1)。

(继承自 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供对某一对象公开的属性和方法的访问。

(继承自 Attribute)

适用于

另请参阅