.NET Framework Class Library
WebResourceAttribute Class

Defines the metadata attribute that enables an embedded resource in an assembly. This class cannot be inherited.

Inheritance Hierarchy
SystemObject
   SystemAttribute
    System.Web.UIWebResourceAttribute

Namespace:   System.Web.UI
Assembly:  System.Web (in System.Web.dll)
Syntax
<[%$TOPIC/bzewcx2t_en-us_VS_110_2_0_0_0_0%]([%$TOPIC/bzewcx2t_en-us_VS_110_2_0_0_0_1%].Assembly, AllowMultiple := True)> _
Public NotInheritable Class WebResourceAttribute _
	Inherits [%$TOPIC/bzewcx2t_en-us_VS_110_2_0_0_0_2%]
[[%$TOPIC/bzewcx2t_en-us_VS_110_2_0_1_0_0%]([%$TOPIC/bzewcx2t_en-us_VS_110_2_0_1_0_1%].Assembly, AllowMultiple = true)]
public sealed class WebResourceAttribute : [%$TOPIC/bzewcx2t_en-us_VS_110_2_0_1_0_2%]
[[%$TOPIC/bzewcx2t_en-us_VS_110_2_0_2_0_0%]([%$TOPIC/bzewcx2t_en-us_VS_110_2_0_2_0_1%]::Assembly, AllowMultiple = true)]
public ref class WebResourceAttribute sealed : public [%$TOPIC/bzewcx2t_en-us_VS_110_2_0_2_0_2%]
[<[%$TOPIC/bzewcx2t_en-us_VS_110_2_0_3_0_0%]>]
[<[%$TOPIC/bzewcx2t_en-us_VS_110_2_0_3_0_1%]([%$TOPIC/bzewcx2t_en-us_VS_110_2_0_3_0_2%].Assembly, AllowMultiple = true)>]
type WebResourceAttribute =  
    class 
        inherit [%$TOPIC/bzewcx2t_en-us_VS_110_2_0_3_0_3%] 
    end

The WebResourceAttribute type exposes the following members.

Constructors
  NameDescription
Public method WebResourceAttributeInitializes a new instance of the WebResourceAttribute class with the specified Web resource and resource content type.
Top
Properties
  NameDescription
Public property CdnPathGets or set the path of a Content Delivery Network (CDN) that contains Web resources.
Public property CdnSupportsSecureConnectionGets or set a value that indicates to the ScriptManager whether a script resource should be accessed using a secure connection to the content delivery network (CDN) path when the page is accessed using HTTPS.
Public property ContentTypeGets a string containing the MIME type of the resource that is referenced by the WebResourceAttribute class.
Public property LoadSuccessExpressionGets or sets an expression that is used when a Web resource has successfully loaded.
Public property PerformSubstitutionGets or sets a Boolean value that determines whether, during processing of the embedded resource referenced by the WebResourceAttribute class, other Web resource URLs are parsed and replaced with the full path to the resource.
Public property TypeIdWhen implemented in a derived class, gets a unique identifier for this Attribute. (Inherited from Attribute.)
Public property WebResourceGets a string containing the name of the resource that is referenced by the WebResourceAttribute class.
Top
Methods
  NameDescription
Public method EqualsInfrastructure. Returns a value that indicates whether this instance is equal to a specified object. (Inherited from Attribute.)
Public method GetHashCodeReturns the hash code for this instance. (Inherited from Attribute.)
Public method GetTypeGets the Type of the current instance. (Inherited from Object.)
Public method IsDefaultAttributeWhen overridden in a derived class, indicates whether the value of this instance is the default value for the derived class. (Inherited from Attribute.)
Public method MatchWhen overridden in a derived class, returns a value that indicates whether this instance equals a specified object. (Inherited from Attribute.)
Public method ToStringReturns a string that represents the current object. (Inherited from Object.)
Top
Explicit Interface Implementations
  NameDescription
Explicit interface implemetation Private method _AttributeGetIDsOfNamesMaps a set of names to a corresponding set of dispatch identifiers. (Inherited from Attribute.)
Explicit interface implemetation Private method _AttributeGetTypeInfoRetrieves the type information for an object, which can be used to get the type information for an interface. (Inherited from Attribute.)
Explicit interface implemetation Private method _AttributeGetTypeInfoCountRetrieves the number of type information interfaces that an object provides (either 0 or 1). (Inherited from Attribute.)
Explicit interface implemetation Private method _AttributeInvokeProvides access to properties and methods exposed by an object. (Inherited from Attribute.)
Top
Remarks

The WebResourceAttribute class is valid only when used on assembly declarations. It is used to enable a specified embedded resource in an assembly for use as a Web resource.

For more information on resources, see ASP.NET Web Page Resources Overview.

Examples

This section contains two code examples. The first code example demonstrates how to apply the WebResourceAttribute attribute to a namespace that defines a custom control, MyCustomControl. The second code example demonstrates how to use the MyCustomControl class in a Web page.

The following code example demonstrates how to apply the WebResourceAttribute attribute on a custom assembly to define an image Web resource and an HTML Web resource. The MyCustomControl class defines a composite control that uses the resources to set the value of the ImageUrl property of an Image control that is contained within the composite control and to set the HRef property of an HtmlAnchor control linking to the HTML resource.

Imports Microsoft.VisualBasic
Imports System
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
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);

		}
	}

}

The following code example demonstrates how to use the MyCustomControl class in a Web page.

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

This example requires that you compile the Image1.jpg and Help.htm resources with the assembly that contains MyCustomControl. For more information, see, /resource (C# Compiler Options) or /resource (Visual Basic).

An example of an HTML Web resource that could be used in this example is shown next. Note the use of the WebResource syntax, which is used when you set the PerformSubstitution property to true for a Web resource.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >

<head>

<title>Included Help Page</title>

</head>

<body>

<div>

<img alt="help image" src=<% = WebResource("image1.jpg") %> />

Included help file.

</div>

</body>

</html>

Version Information

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0
Platforms

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Thread Safety
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.