Using WebResource.axd for embedded resources

Wow, and actual .NET post. I must not be feeling well.

Anyway, anyone that has tried to use embedded resources in compiled assemblies using ASP.NET v2 has probably had the urge to beat their head against the wall. The documentation, as of now, pretty much sucks. However, after using our friend Reflector, I think I get what's going on now.

Say that you're building a control and you want to embed an image in the compiled assembly. Then you want to use the handy built-in HttpHandler WebResource.axd to serve that bad boy up. I've seen at least three different stories indicating the way it should work, but I couldn't get any of them to work. This is what worked for me:

1. Add the image to your project.
2. In the solution explorer, click the file, and in the property window, change build action to "embedded resource."
3. Add the following to your AssemblyInfo.cs file:
[assembly: WebResourceAttribute("MyNameSpaces.Resources.MyImage.gif", "image/gif")]
Important note here... the "MyNameSpaces" comes from the default namespace indicated in the project properties. The "Resources" comes from the fact that I happened to put the image file in the "Resources" folder I made.
4. In your control rendering code, you'll need the following to produce a usable URL for the image:
Page.ClientScript.GetWebResourceUrl(typeof(MyNameSpaces.MyControl), "MyNameSpaces.Resources.MyImage.gif")
Notice I used the same naming convention here.
5. Compile away.

This should produce a URL in your rendered control that looks something like:
/WebResource.axd?d=PhPk80h_UWEcbheb-NHNP5WshV_47UOpWqAOl1_li
UFfN4cNofL74cFlQ1fvpFSf0&t=632573240669964903


Now, the issue that I have with this is that I'm sure it involves some kind of reflection or something, and frankly I don't know if it's caching the object/stream/graphic. One of the instructions I saw said to add the images as resources in the project property page, but doing so only generated a Properties\Resources.cs file with more mangled name spaces. So if there's a more "correct" way to do this, someone please share!


20 Comments

  • Also see bug report (and workaround) on this issue on http://lab.msdn.microsoft.com/productfeedback/viewfeedback.aspx?feedbackid=c1ca82bd-ee99-422d-8933-d88e7e7abb7e

  • "Page.ClientScript" does not compile in the assembly

  • Is it any surprise? This is .net hud we're talking about here...

  • I've been playing around with this myself and got stung with a namespace issue.
    Here's what I learned
    I put the resources into a Resource subfolder in my Custom Control project. My resources are in subfolders of this Resource subfolder.
    I then added the resources to a new Resource file using the resource editor (I thought this was required, not sure of the benefits of this besides the ability to use a Resource manager class or something to access the contents, please let me know if it's required in this case).
    Even though the resources are in the Resource file, .resx you still need the full path to the resource.
    e.g.
    My resources are in
    /Resources/javascript/script1.js
    /Resources/javascript/components/script2.js

    You must use the full path to reference

    AssemblyInfo.cs

    [assembly: WebResource("WebControlLibrary.Resources.javascript.script1.js", "text/javascript", PerformSubstitution = true)]
    [assembly: WebResource("WebControlLibrary.Resources.javascript.components.script2.js", "text/javascript", PerformSubstitution = true)]



    and in the Custom Control access using

    Page.ClientScript.RegisterClientScriptInclude(this.GetType(), "applicationscript", Page.ClientScript.GetWebResourceUrl(this.GetType(), "WebControlLibrary.Resources.javascript.script1.js"));


    Page.ClientScript.RegisterClientScriptInclude(this.GetType(), "applicationscript", Page.ClientScript.GetWebResourceUrl(this.GetType(), "WebControlLibrary.Resources.javascript.components.script2.js"));


  • Can anyone tell me why hitting F5 causes a web resource to be downloaded ? Here are some of the things I have observed using fiddler.

    I have a page with a few compiled js resources being gotten through WebResource.axd. I also included some non compiled js resources and images (just straight img and script tags). If these items have been cached opening a new browser and requesting the page causes them to be retrieved from the browser cache, but if I hit F5 only the noncompiled resources are retrieved from browser cache. The WebResorces are gotten again. This doesn't make sense to me. CTRL+F5 should cause cache overwrite not F5.

  • Remember that sometimes the IIS or Casini webserver needs to be restarted in order for this to work.

  • When using embedded resources with images
    and u get the following error
    Illegal Character
    Line : 1
    Code : 0

    it means that u
    u have Registered a script Resource
    ClientScript.RegisterClientScriptResource

    Just use
    ClientScript.GetWebResourceUrl

  • Another good article on the subject.

    They don't use typeof, but i guess this is the same thing:

    ClientScriptManager cs = Page.ClientScript;
    Type rsType = this.GetType();

    http://support.microsoft.com/kb/910442

  • this worked great! Saved me a ton of time I was ready to give up on using embeded resources.

  • HI, Question, I have a table control for rendering round corners, square corners and combination's of the two like the rounded corner panel, except with quality corners, thing is I want it to be a dll, or something like it that I can lug round into other projects, problem is that I will need to store the CSS, (Makes the control much lighter), and the images referenced by the CSS in the web resource, I haven't used them before so not really sure how that works?

  • @Natewon - Just because you used a "?" it does not mean that your run on sentence was a question.

  • Hi:

    I have a great doubt about on how do I to show an image contained in an external dll?

    I've just created a external dll (resources.dll) that contains 16000 images as resources. I've included this in my current web project by copying it the bin folder.

    By running this:
    Assembly dll = Assembly.LoadWithPartialName("resources");
    string [] resources = dll.GetManifestResourceNames();
    dgPages.DataSource = resources;
    dgPages.DataBind();
    I got a table listing my images names. So far so good!

    But, how do I to show one of these in a img tag?

    something like this doesn't work:
    imgTest.ImageUrl = resources[1];

    Any help well be so appreciated!

    Regards

  • Read the post... you're doing it wrong. And what you're doing is a cosmically bad idea.

  • Is there any way that you can possible take for example a video from youtube, take the embed code from the top right and use that in a project?

  • Ok so i've read 10 posts on this subject, including this one and the on mentioned here from Microsoft, which btw is very good.

    BUT... (always a but isnt there?)


    I have a web server control which I want to use in my toolkit. The control needs js and css. I want to have a drag and drop tool so the css file and teh js file need to be INSIDE the dll.

    I want 1 dll and no externally hosted files.

    I actually would like my dll to generate a css and a js file when i drag the tool on my aspx.

    No inpage or inline script or css, just a neat link to the js and script in my head tag.

    Does something like that even exist or is possible?

    Greetz NiQuil

  • So what's the problem? This article explains how to embed this stuff.

  • Thank you so much for this post. I was planning on having to spend half a day on this embedded resource nonsense, but instead it took me 15 minutes!

  • No more comments i am fed up now.

  • Saved me so much time. Thanks for the post!

  • function __doPostBack(eventTarget, eventArgument) {
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
    theForm.__EVENTTARGET.value = eventTarget;
    theForm.__EVENTARGUMENT.value = eventArgument;
    theForm.submit();
    }
    }

Comments have been disabled for this content.