pavanarya

Let us share the knowledge

Web Resources.axd

with 3 comments

Generally we will be having all the static content files like Java scripts, images, css files at different locations on our machine and we will be importing them in our aspx or html pages by mentioning the path where they exist.

For example let us assume that i am having a java script file in a location like c:\Pavan\Proj\Java Script\Example.js and similarly some other static files like images and css at different location on my machine and i will be making use of these files by specifying the exact path in my aspx. Up-to now everything is fine. But i am planning to deploy my application onto a different server. In this there are more chances of me facing the issue with the path of these static files i am making use in my application. We should take care that these static files are placed in the similar location like in my first environment.

So in order to over come these type of issues we can make use of WebResources.

The main theme of web resources is we can embed all the static files into an assembly(dll) so that we can avoid the problems mentioned in the above scenario. Now let us see how to create Web Resource files and how to make use of them.

Embedding static files into a dll:

1. Create a classlibrary project in visual studio.

2. Add static files like js,image,css files

3. Select the static files and click on properties.

4. Open the AsseblyInfo.cs file and add the webreference attribute

[assembly: WebResource("ClassLibrary1.Testjavascript.js", "text/javascript")]
[assembly: WebResource("ClassLibrary1.testcssfile.js", "text/css")]

5. We will have Web resource attribute class in System.Web.UI
The WebResource assembly attribute has three parameters as follows:
1.Web Resource: The name of the resource that is embedded in the assembly
2.ContentType: The MIME file type for the resource
3.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
Now compile the classLibrary project and in the resultant dll we will have these static file embedded.
We can see these embedded file by using software like Reflector.


Now we are having a dll in which the static files were embedded. So the next step would be to make use of them in our project.

The following are the steps to make use of these resources.

1. Add the dll(that contains static data) or proj reference to the website we are developing.

2. So inorder to access the resource that is present in the dll we should make use of a function called GetWebresourceUrl() which is  a method of the class “ClientScriptManager” which is used to bind the client scripts.

string imagepath = Page.ClientScript.GetWebResourceUrl(t, "ClassLibrary1.tig.jpg");
img1.Src = imagepath;

So the GetWebResourceUrl() takes 2 parameters.

i. Type

Type t = typeof(Namespace.ClassName);

ii. Complete name of the resource we are trying to get.

3. So the GetWebResourceUrl() returns a string and we can assign that string to the HtmlControl as src.

Type t = typeof(ClassLibrary1.Class1);
      ClientScript.RegisterClientScriptInclude("test", Page.ClientScript.GetWebResourceUrl(t, "ClassLibrary1.Testjavascript.js"));
      string imagepath = Page.ClientScript.GetWebResourceUrl (t, "ClassLibrary1.tig.jpg");
      img1.Src = imagepath;

ClientScript.RegisterClientScriptInclude() is used to register the javascript from the code behind file.

 

 

 

 

 

 

 

 

 

About these ads

Written by pavanarya

November 3, 2011 at 5:09 pm

Posted in Asp.net

3 Responses

Subscribe to comments with RSS.

  1. […] « Web Resources.axd […]

  2. Thank you for giving the info about why scriptresource.axd file generated while building my website skillgun i faced the same problem

    Lokanatha

    September 14, 2013 at 4:12 pm

    • Thank you so much Lokanatha … For your kind words… i am glad that it helped you a lot

      pavanarya

      September 15, 2013 at 8:09 am


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 69 other followers

%d bloggers like this: