Wednesday, January 27, 2010

How Web Resources work

Web 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:
WebResource.axd?d=SbXSD3uTnhYsK4gMD8fL84_mHPC5jJ7lfdnr1_WtsftZiUOZ6IXYG8QCXW86UizF0&t=632768953157700078

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 code
In 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 Resource
For creating and embedding a Web Resource, I have developed a COM control (SimpleControl). It has the following embedded resources:

* Two image files that are named smallFail.gif and smallSuccess.gif. These image files are used for rollover effects in the control and are consumed within the control code.
* An HTML file that is named Help.htm. This file does not contain much text and is just for demonstration.
* One JavaScript file that is named MyScript.js. This file contains some JavaScript code which also demonstrates the substitution feature to get a reference to another embedded resource within the same assembly.
* One .css file that is named MyStyleSheet.css. This style sheet is then consumed directly from the ASP.NET Web application.

In this article, I will discuss the parts that are required for Web Resources.
Embedding the Web Resources
First, 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
he WebResource assembly attribute has three parameters as follows:

* Web Resource: The name of the resource that is embedded in the assembly
* ContentType: The MIME file type for the resource
* PerformSubstitution: A Boolean value that determines whether other Web Resource URLs that are referenced in this resource are parsed and replaced with the full path of the resource

Fetching the Web Resources
For 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:

* Type: The type of the server-side resource
* Resource Name: The name of the server-side resource

To use this method, first you have to create an instance of the ClientScriptManager class and get the type of the class as shown below.
ClientScriptManager cs = Page.ClientScript;
Type rsType = this.GetType();

No comments:

Post a Comment