WebResourceAttribute Class

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

Namespace: System.Web.UI
Assembly: System.Web (in system.web.dll)

<AttributeUsageAttribute(AttributeTargets.Assembly, AllowMultiple:=True)> _
Public NotInheritable Class WebResourceAttribute
	Inherits Attribute
Dim instance As WebResourceAttribute

/** @attribute AttributeUsageAttribute(AttributeTargets.Assembly, AllowMultiple=true) */ 
public final class WebResourceAttribute extends Attribute
AttributeUsageAttribute(AttributeTargets.Assembly, AllowMultiple=true) 
public final class WebResourceAttribute extends Attribute
Not applicable.

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.

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")

            ' Create a new Label control.
            Dim _lab As New Label()
            _lab.Text = "A composite control using the WebResourceAttribute class."

            ' 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 />"))

        End Sub
    End Class

End Namespace

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

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>WebResourceAttribute Example</title>
    <form id="form1" runat="server">
      <AspNetSamples:MyCustomControl id="MyCustomControl1" runat="server">

This example requires that you compile the Image1.jpg and Help.htm resources with the assembly that contains MyCustomControl. For more information, see, /resource (Embed Resource File to Output) (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" >


<title>Included Help Page</title>




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

Included help file.




  • AspNetHostingPermission  for operating in a hosted environment. Demand value: LinkDemand; Permission value: Minimal.


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

Windows 98, Windows Server 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0

Community Additions