Using the ASP.NET 2.0 Web Resource Attribute
ASP.NET 2.0 has a really nice feature that lets you compile site or component resources like javascript files into your assembly file instead of deploying these shared files to the wwwroot/aspnet_client directory like in ASP.NET 1.x. This makes your component/website much easier to deploy because it means you can keep everything you need in one file.
It’s pretty straight forward to do too (when you know how). You basically just need to add an embedded resource to your project, reference it in AssemblyInfo.cs using the WebResourceAttribute attribute and then use Page.ClientScript.GetWebResourceUrl() wherever you need to reference the file.
It took me ages to figure out how to get all the references right, so here are some more detailed step by step instructions on how to use it in your project.
1. Add resource to the project. You can put it in a subdirectory of the main project if you like, but this affects how the resource will be named.
2. Set the resource’s Build Action property to Embedded resource.
3. Add a WebResourceAttribute attribute to your AssemblyInfo.cs file (usually found in the Properties directory of your project).
You need to give the fully qualified name of the resource, which will include the default namespace of your project followed by the relative path to the file with directory slashes replaced by full stops. You will also need to provide a content type that will be sent to the browser:
[assembly: WebResourceAttribute("Examples.WebResource.Images.Elephant.jpg", "image/jpg")]
If you’re unsure about what the fully qualified name of your resource is, you can add some code to one of the class files in the project that will write the name of each embedded resource in the assembly to the debug output stream when the code is run:
string[] resources =
this.GetType().Assembly.GetManifestResourceNames();
foreach (string resourceName in resources)
{
Debug.WriteLine(resourceName);
}
4. Use the Page.ClientScript.GetWebResourceURL() method to reference your resource wherever you want to refer to it in your project. You will need to provide the same fully qualified name that you used to declare it in AssemblyInfo.cs:
this.ImgSrc =
Page.ClientScript.GetWebResourceUrl(this.GetType(),
"Examples.WebResource.Images.Elephant.jpg");
References:
Handling Client Files in ASP.NET 2.0
Nikhil Kothari’s overview of WebResourceAttribute
WebResourceAttribute API reference

January 30th, 2006 at 12:33 pm
This is FIRST page which help me to see objects from my resources, just the FULL name:)
JM
March 2nd, 2006 at 2:06 am
You’re a blog of an angel! Thank you so much for your wonderful description of using this WebResource correctly!
May 17th, 2006 at 2:03 am
Thanks for the nice blog entries they are quite helpful. One other thing I have realized, but have not seen documented in too many places is that the resource name is case sensitive unlike a normal URL.
For example the following two calls are to different resources.
Page.ClientScript.GetWebResourceUrl(this.GetType(), “Examples.WebResource.Images.Elephant.jpg”);
Page.ClientScript.GetWebResourceUrl(this.GetType(), “Examples.WebResource.Images.elephant.jpg”);
November 10th, 2006 at 1:51 am
Beware when using this.GetType(). If your class is inherited, “this” will reference the inheriting class which may be contained in a different assembly. If the inheriting class is in a different assembly, then you’ll receive a 404 File Not found error when trying to retrieve the resource. Use typeof(ClassName) instead. This will solve the inheritance issues.
September 4th, 2006 at 1:01 am
Great article!
I’m trying to use this with a UserControl (compiled into a dll) but the image never appears. I’m sure the resource name is correct so i don’t know what’s wrong. Is it possible to use image resources with usercontrols?
Thank you.
May 16th, 2007 at 3:58 am
Hi Netflash
I had the same issue like you. With some modifications i made it to work. The main problem is that the Control has it’s own Page Object/Assembly.
You need to go to the Parent’s Page Object.
So, instead of using:
Page.ClientScript.GetWebResourceUrl(this.GetType(), “Examples.WebResource.Images.Elephant.jpg”);
try this:
this.Parent.Page.ClientScript.GetWebResourceUrl(this.Parent.GetType(), “Examples.WebResource.Images.Elephant.jpg”);
I hope this will do
Greets David
December 18th, 2007 at 5:29 am
I have a slight twist to all the previously discussed.
I don’t have a problem seeing my embedded resources that are built into my DLL.
I cannot seem to get the resources to be pathed with the full folder location.
I have my images nested at:
Namespace\Folder1\Folder2\imagex.gif
When I use the above GetManifestResourceNames() function, i can see my images, but their pathing is:
Namespace\imagex.gif
My folders are dropped.
I have looked for a build setting, or project property to change to make use of full pathing.
I’m doing this in VB.NET, and I have previously done this task successfully in C#.
I’m not convinced theres that much of a difference in application of the two languages, and am looking for anyone who might know of a pre-requisite that I might have missed.
Thanks in advance
Benn
December 18th, 2007 at 6:00 am
OK
Further to my previous post today, it seems that other VB’ers have hit the same wall.
Look at the following codeproject postings under this C# worked example. It talks in a couple of posts as to some fundamental differences experienced with trying to implement a straight code conversion from C# to VB.
http://www.codeproject.com/KB/aspnet/MyWebResourceProj.aspx?df=90&forumid=265875&exp=0&select=1468303&fid=265875&mpp=25&noise=3&sort=Position&view=Quick&fr=26#xx1468303xx
Hope this saves any other VB.NET coders from tearing out as much hair as I have this past day :o)
Benn
October 2nd, 2008 at 3:48 am
All it doesnt related to ASP.NET 2.0 and VS2008 – all of you describe there is absent in VS2008 (there is no option “Build Action” in VS2008)
October 10th, 2008 at 6:27 am
Vadim, there’s such an option, but in VS2008 SP1. Also make sure it’s a Web Application, not Web Site.