Telerik blogs

Every now and then I see people asking one of the following questions:

I will try to answer them in a series of blog posts. I will also give some details what web resources are, how they work and what are they good for.

Web Resources 101

Web Resources are files which are embedded in an assembly. The WebResource.axd HTTP handler loads them and serves them to the browser. Web Resources were introduced in ASP.NET 2.0. If you are using client-side validators, cross page postback or ASP.NET Ajax you are already working with Web Resources.

Consider the following simple page:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml">  
<head runat="server">  
    <title>Untitled Page</title> 
</head> 
<body> 
    <form id="form1" runat="server">  
    <div> 
        <asp:TextBox runat="server" ID="TextBox1"></asp:TextBox> 
        <asp:RequiredFieldValidator runat="server" ControlToValidate="TextBox1" ID="RequiredFieldValidator1"></asp:RequiredFieldValidator> 
    </div> 
    </form> 
</body> 
</html> 
 

You will see this in the rendered page output:

<script src="/WebSite1/WebResource.axd?d=Vk5UneWKBDpkA9ciMJ9Yq4Xs-QqnxP_LjtDYOIm-fIg1&amp;t=633333579827766977" type="text/javascript"></script>
 

This is how a Web Resource URL looks like. In this particular page this URL points to an embedded JavaScript file which contains the ASP.NET client-side validation code. The file is embedded inside the System.Web.dll assembly and is called “WebUIValidation.js”. This file will be registered when client-side validators are used. Of course the file is included only once to avoid unnecessary HTTP requests.

The highlighted  part of the URL will be different for different Web Resources. It contains some encoded arguments used to uniquely identify the Web Resource so the WebResource.axd HTTP handler can locate it and serve it. That unique identifier is made up of the embedded resource name (e.g. “WebUIValidation.js”) and the assembly name (e.g. “System.Web.dll”). The “t=633333579827766977” part is basically a timestamp and is used to avoid versioning conflicts. For example if a web site starts using a different version of the assembly the Web Resource URL will change. More info can be found in Nikhil Kothari’s blog post about Web Resources. This blog post describes how to find out what web resources are currently registered in an ASP.NET web page.

The WebResource.axd HTTP handler is registered in machine.config. Hence Web Resources are available in every ASP.NET web site. ASP.NET Ajax introduces a new Web Resource HTTP handler -ScriptResource.axd. It behaves in the same way as WebResource.axd – it loads embedded resources based on the URL and serves them back to the browser. There is one important difference though – ScriptResource.axd compresses the embedded resources using the GZIP protocol so the client browser downloads less bytes.

RadControls for ASP.NET “Classic” utilize the ScriptResource.axd handler provided there is a ScriptManager control on the page.  Being native ASP.NET Ajax controls, RadControls “Prometheus” always use the ScriptResource.axd handler.

Benefits from using Web Resources

Web Resources greatly facilitate the deployment of file based resources such as JavaScript, CSS and images. In the past those resources were placed in a central location (e.g. inetpub\wwwroot\aspnet_client) or were copied in the web application folder (the RadControls folder used by ASP.NET 1.x RadControls “Classic”). Rich ASP.NET control libraries depend on file resources – CSS, JavaScript, images. It is a lot easier to deploy and upgrade just a few dll files instead of hundreds other files. Web Resources also resolve potential versioning conflicts – a new version of the dll using older JavaScript files. Web Resources also play nicely with Visual Studio and are easily accessible in design time.

Stay tuned for more Web Resource related blog posts.


About the Author

Iana Tsolova

is Product Manager at Telerik’s DevTools division. She joined the company back in the beginning of 2008 as a Support Officer and has since occupied various positions at Telerik, including Senior Support Officer, Team Lead at one of the ASP.NET AJAX teams and Technical Support Director. Iana’s main interests are web development, reading articles related to geography, wild nature and latest renewable energy technologies.

Comments

Comments are disabled in preview mode.