Jeff and .NET

The .NET musings of Jeff Putz

Sponsors

News

My Sites

Archives

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!


Posted: Jul 18 2005, 11:44 PM by Jeff | with 24 comment(s)
Filed under:

Comments

dion said:

# July 19, 2005 6:41 PM

Dee said:

"Page.ClientScript" does not compile in the assembly

# September 10, 2006 3:44 PM

Mark Hildreth said:

Man, that namespaces thing is a real gotcha.

# October 12, 2006 11:23 AM

RR said:

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

# October 19, 2006 10:56 AM

learnerplates said:

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

<project>/Resources/javascript/script1.js

<project>/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"));

# October 27, 2006 5:34 AM

fabal said:

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.

# June 4, 2007 5:07 PM

Johan said:

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

# July 5, 2007 10:09 AM

Paraesthesia said:

EmbeddedResourcePathProvider - Binary-Only ASP.NET 2.0

# July 13, 2007 2:56 PM

Neil said:

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

# November 2, 2007 9:30 AM

Jobu said:

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();

support.microsoft.com/.../910442

# March 14, 2008 2:18 PM

PittGeek » Blog Archive » WebResource.axd mystery solved said:

Pingback from  PittGeek  &raquo; Blog Archive   &raquo; WebResource.axd mystery solved

# May 7, 2008 3:22 PM

gotabyte said:

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

# July 15, 2008 4:47 PM

Natewon said:

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?

# October 1, 2008 10:15 PM

Smeggles said:

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

# October 20, 2008 11:56 AM

Mirta said:

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?

<asp:Image ID="imgTest" runat="Server" />

something like this doesn't work:

imgTest.ImageUrl = resources[1];

Any help well be so appreciated!

Regards

# November 4, 2008 11:55 AM

Jeff said:

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

# November 4, 2008 12:09 PM

David said:

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?

# December 21, 2008 1:07 AM

NiQuil said:

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

# January 27, 2010 5:45 AM

Jeff said:

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

# January 27, 2010 11:31 AM

Rob said:

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!

# May 6, 2010 10:41 AM

anonymous said:

No more comments i am fed up now.

# September 15, 2010 5:57 AM

Colby said:

Saved me so much time.  Thanks for the post!

# January 27, 2011 3:12 PM

ss said:

function __doPostBack(eventTarget, eventArgument) {

   if (!theForm.onsubmit || (theForm.onsubmit() != false)) {

       theForm.__EVENTTARGET.value = eventTarget;

       theForm.__EVENTARGUMENT.value = eventArgument;

       theForm.submit();

   }

}

# March 1, 2011 6:24 AM
Leave a Comment

(required) 

(required) 

(optional)

(required)