Article ID: 910442 - Last Review: January 15, 2007 - Revision: 1.3 Working with Web Resources in ASP.NET 2.0ASP.NET Support Voice ColumnWorking 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.On This PageIntroductionHi! 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. You can download the sample code in this article from the Microsoft Download Center. The following files are available for download from the Microsoft Download Center: Collapse this image ![]() Collapse this image ![]() 119591
(http://support.microsoft.com/kb/119591/
)
How to obtain Microsoft support files from online services
Microsoft scanned this file for viruses. Microsoft used the most
current virus-detection software that was available on the date that the file
was posted. The file is stored on security-enhanced servers that help prevent
any unauthorized changes to the file.
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: 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.
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:
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. Also, to make sure that this JavaScript file is registered in the page, I use the RegisterClientScriptInclude method of the ClientScriptManager class as shown below. 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. Collapse this image ![]() Collapse 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:ClientScriptManager.GetWebResourceUrl method For more information about Web Resources, see the following blogs
by Nikhil Kothari:http://msdn2.microsoft.com/en-us/library(d=robot)/system.web.ui.clientscriptmanager.getwebresourceurl.aspx (http://msdn2.microsoft.com/en-us/library(d=robot)/system.web.ui.clientscriptmanager.getwebresourceurl.aspx) WebResourceAttribute class http://msdn2.microsoft.com/en-us/library(d=robot)/system.web.ui.webresourceattribute.aspx (http://msdn2.microsoft.com/en-us/library(d=robot)/system.web.ui.webresourceattribute.aspx) Linked vs. embedded resources http://msdn2.microsoft.com/en-us/library/ht9h2dk8.aspx (http://msdn2.microsoft.com/en-us/library/ht9h2dk8.aspx) Whats with System.Web.UI.WebResourceAttribute? http://www.nikhilk.net/WebResourceAttribute.aspx (http://www.nikhilk.net/WebResourceAttribute.aspx) Mitigating Denial of Service attacks with Web Resources http://www.nikhilk.net/WebResourcesDenialOfServiceAttack.aspx (http://www.nikhilk.net/WebResourcesDenialOfServiceAttack.aspx) 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
(http://support.microsoft.com/common/survey.aspx?scid=sw;en;1176&p0=&p1=&p2=&p3=&p4=)
form. | Article Translations
|


Back to the top


