Article ID: 910442 - View products that this article applies to.
ASP.NET Support Voice Column
Working with Web Resources in ASP.NET 2.0To customize this column to your needs, we want to invite you to submit your ideas about topics that interest you and issues that you want to see addressed in future Knowledge Base articles and Support Voice columns. You can submit your ideas and feedback using the Ask For It
(http://support.microsoft.com/common/survey.aspx?scid=sw;en;1176&p0=&p1=&p2=&p3=&p4=)form. There's also a link to the form at the bottom of this column.
IntroductionHi! My name is Praveen Yerneni and I am a Technical Lead working with the ASP.NET support group at Microsoft. In this month's column, I am going to discuss Web Resources and how to implement them in your application.
If you are a developer who works extensively with COM controls, you might find this article useful in addressing some of the key issues that you may face while developing controls. Previously, in Microsoft ASP.NET 1.1, if you wanted to deploy static files such as .html files, .css files, image files, and script files by using a COM control, you had to create an installer and bundle these files together with the COM control. Although this worked, it presented many problems while deploying the control. For example, you may have had problems with version conflicts, with making sure that files such as images were deployed to the same location, or with corrupted or deleted static files that could cause the control to fail.
Web Resources in Microsoft ASP.NET 2.0 addresses these issues by letting the developer embed these resources in the assembly as .resources. To access these static resources, you can just call a method in the control's code.
How Web Resources workWeb Resources rely on a special handler that is named WebResource.axd, which is designed to retrieve assembly resources and serve them to the Web browser. The handler type for WebResource.axd is AssemblyResourceLoader.
When a request comes in from the client for WebResource.axd, the handler looks for the Web Resource identifier in the QueryString method of the Request object. Based on the value of the Web Resource identifier, the handler then tries to load the assembly that contains this resource. If this operation is successful, the handler will then look for the assembly attribute and load the resource stream from the assembly. Finally, the handler will grab the data from the resource stream and send it to the client together with the content type that you specify in the assembly attribute.
The URL for WebResource.axd looks like the following:
The format of this URL is WebResource.axd?d=encrypted identifier&t=time stamp value. The "d" stands for the requested Web Resource. The "t" is the timestamp for the requested assembly, which can help in determining if there have been any changes to the resource.
Delving into the codeIn my example, I have developed a COM control that is consumed by an ASP.NET Web application to demonstrate this new feature.
COM control for the Web ResourceFor creating and embedding a Web Resource, I have developed a COM control (SimpleControl). It has the following embedded resources:
Embedding the Web ResourcesFirst, you have to make sure that all the static files that are added to the Web Control Library project in Microsoft Visual Studio 2005 are embedded resources. To embed these resources, all that you have to do is add these files to Visual Studio and then modify the properties of these files so that the build action is set to Embedded Resource.
After you have done this, you also have to make sure that these resources have been referenced by the WebResource assembly attribute in the AssemblyInfo.cs file as shown below.
The WebResource assembly attribute has three parameters as follows:
Fetching the Web ResourcesFor getting the Web Resource, I have used the GetWebResourceUrl method, which is a method of the ClientScriptManager class that is typically used for managing client-side scripts. This method returns a URL reference to the server-side resource that is embedded in an assembly. The GetWebResourceUrl method accepts the following two parameters:
When you have an instance of this class, you then have to call this method and pass the appropriate parameters as shown below, where I create a HyperLink button, and set the NavigateURL method to point to an embedded HTML resource.
Also in the same code listing above, I have created an Image button and the ImageUrl attribute is getting its value from another embedded .gif image resource.
Using the Substitution featureAnother useful feature of using Web Resources is the PerformSubstitution property of the WebResource attribute as shown in the "Embedding the Web Resources" section above. This feature lets you actually get Web Resources inside another embedded resource. If this property is enabled, the WebResource handler will parse the embedded resource and replace the syntax for the WebResource assembly attribute with actual resources before serving the page.
In the code above, you will notice that the HyperLink control has a client-side event that is named ChangeImage. This ChangeImage function is located in a script file. The following is the code for this script.
This script file that is named MyScript.js is also an embedded resource in the same COM control. And notice that I am using the WebResource method to refer to another embedded resource in the script file.
Consuming the control in ASP.NETOnce you have built the COM control successfully, you need to consume this control in a client ASP.NET project. To do this, you just have to add the control to the toolbox in ASP.NET. Then you can add the control to any Web form in the designer.
When you add the control to the toolbox, the control is registered for you and the following code is added when you look at the HTML syntax.
In my COM control, I have also embedded a style sheet file, which I will be using in my ASP.NET application. To access this style sheet, you have to add a link HtmlControl in the HTML page and set the runat property to server as shown below.
After this, you can programmatically set the Href attribute of this control to point to the WebResource assembly attribute in the Page_Load event as shown below.
When you run the code, you will see a control that looks like the following:
And on mouse-over, the control will look like the following:
Collapse this imageExpand this image
When you click this link, it will take you to the help page, which is embedded in the COM control assembly file.
Collapse this imageExpand this image
ConclusionWeb Resources is a useful new feature in ASP.NET 2.0. You can use this feature to your advantage if you use it wisely. Many caching techniques have been used to make sure that Web Resources do not affect the performance of the application.
In ASP.NET 2.0, WebResource.axd is used internally by the Microsoft .NET Framework to retrieve embedded resources. For example, the .NET Framework uses WebResource.axd to retrieve the WebUIValidation.js script file that is used for Validation controls and to retrieve the Menu.js script file for Menu controls.
ReferencesFor more information, visit the following Microsoft Web sites:
Linked vs. embedded resources
As always, feel free to submit ideas on topics you want addressed in future columns or in the Knowledge Base using the Ask For It
Article ID: 910442 - Last Review: December 20, 2012 - Revision: 3.0
Contact us for more help
Connect with Answer Desk for expert help.