Jeff's Junk

The sillynonsense and .NET musings of Jeff Putz

News

My Sites

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 19 comment(s)
Filed under:

Comments

Nikhil Kothari said:

Jeff, you've got the procedure right. I completely agree - its unfortunate when you have to resort to Reflector to figure things out...

The idea is to embed the image (or script or style sheet or some other content) as an Embedded Resource. For those unfamiliar with how - you add the file to your project, bring up its properties, and in the property grid, change the Build Action to Embedded Resource. You don't want to use the "add images to the resx file" approach here...

This creates a resource named (as you discovered) using the root namespace followed by the folder hierarchy. Typically you'll want to place it resource such that its "namespace" matches the namespace of your control, because the control type is the type passed in into GetWebResourceUrl.

In order to use it you'll want to mark it as web visibile via the WebResource attribute.

The resource is cached - in fact part of the query string goop is to create a URL that the browser can cache indefinitely, because the server changes the query string when the assembly containing the resource has changed. I have some details on my blog (from quite a while ago now) at http://www.nikhilk.net/WebResourceAttribute.aspx and some automatic design-time behavior at http://www.nikhilk.net/WebResourceAttributeAtDesignTime.aspx. Some things have changed, namely the query string arguments, but the general idea has not.

Hope this helps...
# July 19, 2005 4:15 AM

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
Leave a Comment

(required) 

(required) 

(optional)

(required)