Click here to Skip to main content
13,251,398 members (55,778 online)
Click here to Skip to main content
Add your own
alternative version


137 bookmarked
Posted 8 Feb 2006

WebResource ASP.NET 2.0 explained

, 26 Feb 2006
Rate this:
Please Sign up or sign in to vote.
WebResource (rumours and lies exposed): this is how you really do it.


This code drop is part of Redux series. This article started out to be about a dropdown date/time ASP.NET 2.0 server control. So I built the control and all its ancillary JavaScript, along with the images, and then everything was put into a zip file for uploading to the CodeProject. The installation instructions for the control went something like this:

  • Unzip the contents to a folder.
  • Copy the DLL to the Bin folder of your new project.
  • Copy the JavaScript file.
  • Copy the CSS file.
  • Copy the images.

Hey, wait a minute, this is really lame, why all the extra files, why not embed them into the DLL as resources. That was the beginning of three days of hell trying to understand and work with WebResources. I have boiled it down to about twenty lines of working code.

Nothing else on the net gives you an actual working sample and a lot of the information is simply wrong (possibly because it was based on beta versions). Well, this code compiles and runs on the release version of .NET 2.0.

Code snippets

Create a project which looks like this (you can get the control from the download and then just add an ASP.NET project to the solution):

Highlight the three files in MyResources, and in the Properties window, set Build Action to Embedded Resource:

The source for the control is as follows:

namespace MyWebResourceProj
  [ToolboxData("<{0}:MyWebResource runat="server"></{0}:MyWebResource>")]
  public class MyWebResource : System.Web.UI.WebControls.TextBox
      protected override void RenderContents(HtmlTextWriter output)

      protected override void OnInit(EventArgs e)
           this.GetType(), "Test", 

        // create the style sheet control
        // and put it in the document header
        string csslink = "<link href='" + 
           + "' rel='stylesheet' type='text/css' />";
        LiteralControl include = new LiteralControl(csslink);

This is not a discussion on server controls, so let's focus on the parameters of GetWebResourceUrl:

  • this.GetType() - mandatory, just do it.
  • "MyWebResourceProj.MyResources.Test.js" - this is the most misunderstood parameter and the source of just about all errors. It is composed of [Assembly of this project].[Folder containing resource].[Filename of resource].

No matter what you read anywhere else, including Microsoft's official documentation (which is wrong), if you don't do it this way, you will fail (I know this from personal experience).

Now on to the AssemblyInfo.cs:

[assembly: System.Web.UI.WebResource(
      "MyWebResourceProj.MyResources.Test.css", "text/css")]
[assembly: System.Web.UI.WebResource(
      "text/javascript", PerformSubstitution = true)]
[assembly: System.Web.UI.WebResource(
      "MyWebResourceProj.MyResources.Test.gif", "image/gif")]

You need to specify the resource name exactly as you did in your program code. You need to specify the mime-type. If the file contains JavaScript, you may want to have ASP.NET perform text substitution. For example, you may have a line in JavaScript like:

document.write("<img src='Test.gif'>");

When you embed your images in a resource, you no longer know the name of it as it appears in the resource. To fix this, code as follows:

document.write("<img src='<%=WebResource("MyWebResourceProj" + 

All the "official documentation" and all the articles on the web say that you can leave [AssemblyName].[FolderName] out. But they are dead wrong!

Note: if you are using this control as the base class of another, see yc4king's note below.


KonstantinG has pointed out an error which only becomes apparent when your namespace is not the same as your assembly name. Previously, I had indicated that the name of the resource is: [Namespace of this project].[Folder containing resource].[Filename of resource], but really it is [Assembly of this project].[Folder containing resource].[Filename of resource]. As always, the best way to find information on this is by using Lutz Roeder's .NET Reflector available at

SmashGrab / Redux series

I have recently started two series of articles here at CodeProject.

Smash and Grab is intended as a series of short articles on one specific code technique. Redux is intended as a series of longer articles which attempts to reduce a complicated topic (like WebResource) into its basic constituent parts and show that once you have all the information, it isn't really that hard. To find the Smash and Grab articles, search for the keyword SmashGrab. To find the Redux articles, search for the keyword Redux. I welcome any contributions to either series, but please follow the guidelines when submitting articles to either.


So there you have it, everything you need to know to use WebResources. This article documented how to place CSS files in the <head> section, extract JavaScript files, dynamically change the contents of a JavaScript file.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Gary Dryden
Software Developer (Senior)
Canada Canada
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralRe: [Assembly of this project].[Folder containing resource].[Filename of resource] Pin
stmarti22-Jan-07 6:01
memberstmarti22-Jan-07 6:01 
GeneralRe: [Assembly of this project].[Folder containing resource].[Filename of resource] Pin
cueshot4-Jun-07 10:31
membercueshot4-Jun-07 10:31 
GeneralRe: [Assembly of this project].[Folder containing resource].[Filename of resource] Pin
cueshot14-Jun-07 14:44
membercueshot14-Jun-07 14:44 
GeneralCheck that embedded resource exist before call Pin
Michael Freidgeim1-Sep-06 18:25
memberMichael Freidgeim1-Sep-06 18:25 
GeneralRe: Check that embedded resource exist before call Pin
stmarti22-Jan-07 6:25
memberstmarti22-Jan-07 6:25 
GeneralEmbedding an aspx Pin
Andrew de Klerk24-Aug-06 0:14
memberAndrew de Klerk24-Aug-06 0:14 
GeneralRe: Embedding an aspx Pin
Adam Selene7-Sep-06 15:47
memberAdam Selene7-Sep-06 15:47 
GeneralRe: Embedding an aspx Pin
Andrew de Klerk7-Sep-06 22:02
memberAndrew de Klerk7-Sep-06 22:02 
Why would i want to? I was wanting to embed a page that acted as a print preview on a dynamically generated image (i.e. at any point you never know what the image is called). So what I would do is open up this new page, regenerate the image and load it up (only bigger and formatted for printing) and then reference it using server.mapPath(Request.applicationPath &.........). I have worked around this by generating a temporary html file which displays all the data after the server calls. I was not wanting to go that route since I would then need to write code to delete the temporary files older than x hours, thereby affecting page load or page unload performance, but alas i had to. If I had an embedded server page, I would simply need one page with a Session reference to the image (or something like that). I still believe it must be possible as the freetextbox ( control has an embeeded page with a image picker whic allows you to upload images etc etc.. But no worries and thanks for the response
AnswerRe: Embedding an aspx Pin
Talk To The Hand6-Apr-07 22:06
memberTalk To The Hand6-Apr-07 22:06 
GeneralWrite out variabel or text in Embedded resource Pin
Skoder12-Aug-06 6:55
memberSkoder12-Aug-06 6:55 
QuestionBrowser Caching??? Pin
paul.tyng21-Jul-06 9:42
memberpaul.tyng21-Jul-06 9:42 
AnswerRe: Browser Caching??? Pin
Adam Selene7-Sep-06 16:00
memberAdam Selene7-Sep-06 16:00 
JokeSmall note Pin
Artur M.22-May-06 3:04
memberArtur M.22-May-06 3:04 
QuestionRe: Small note Pin
oujima6714-Jul-06 15:13
memberoujima6714-Jul-06 15:13 
GeneralRe: Small note Pin
Adam Selene7-Sep-06 16:03
memberAdam Selene7-Sep-06 16:03 
AnswerRe: Small note Pin
gedw9922-Mar-07 11:49
membergedw9922-Mar-07 11:49 
GeneralEmbedded javascript not working. Pin
Loophole5-May-06 13:37
memberLoophole5-May-06 13:37 
GeneralRe: Embedded javascript not working. Pin
Weninger22-May-06 3:50
memberWeninger22-May-06 3:50 
GeneralWebResource access issues in VB.Net Pin
amank30-Apr-06 11:17
memberamank30-Apr-06 11:17 
QuestionRe: WebResource access issues in VB.Net Pin
Venkatesh Mookkan15-Dec-06 1:19
memberVenkatesh Mookkan15-Dec-06 1:19 
QuestionPage instance is null? Pin
asvannat20-Apr-06 7:22
memberasvannat20-Apr-06 7:22 
AnswerRe: Page instance is null? Pin
Loophole5-May-06 13:44
memberLoophole5-May-06 13:44 
GeneralRe: Page instance is null? Pin
asvannat8-May-06 8:47
memberasvannat8-May-06 8:47 
QuestionWhat is the url of a WebResource? Pin
humble.apprentice7-Apr-06 6:11
memberhumble.apprentice7-Apr-06 6:11 
AnswerRe: What is the url of a WebResource? Pin
Loophole5-May-06 14:00
memberLoophole5-May-06 14:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171114.1 | Last Updated 26 Feb 2006
Article Copyright 2006 by Gary Dryden
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid