ASP.NET: Embed Images/JavaScript in a dll

by Brad 31. January 2008 08:42

Its good practice to separate out code that can be re-used into its own class library which can easy be referenced by other projects. Until now any resources such as images/JavaScript files that I'd created I needed to remember to copy to each web site that used the dll, ensuring that I created the correct directory structure so the paths would be correct. Then it dawned on me that you must be able to embed these files into the dll itself. Here's the steps I used to embed an image file for a AjaxControlTookit Calendar Extender that I was using in a custom server control (the same principle can be applied to any file you need - typically JavasScript and/or images will be embedded)

Step 1:

Add your file to the Class Library project

(in my example I added [PROJECT ROOT]/Resources/Calendar.png")

Step 2:

Select the newly added file and in the Properties window select:

Build Action = "Embedded Resource";

Step 3:

In the Project's AssemblyInfo.cs file add the following line:

//Replace "BradsNamespace" with the namespace of your project
//Replace "Resources" with the folder structure from the root of the project 
//to the file (using . instead of / )
//Replace "Calendar.png" with your file's name //Replace "img/png" with relevant type e.g. "text/javascript", "image/jpeg" etc [assembly: System.Web.UI.WebResource(
    "BradsNamespace.Resources.Calendar.png", "img/png")]

Step 3:

Now retrieve the Url for the file:

Image CalendarImage = new Image();

CalendarImage.ImageUrl = Page.ClientScript.GetWebResourceUrl(
 this.GetType(),
 //Replace following with the string entered in AssemblyInfo.cs
 "BradsNamespace.Resources.Calendar.png");

Finished:

And that's it! Now when you run it .NET will generate the Url for you in the format:

src="/WebResource.axd?d=_Y_kbnu3Fp4-fBKDGxpOa-LgnKcgPOIASf1ExUjshsj3373650125705280" 

Tags:

ASP.NET AJAX | C#

Comments

1/31/2008 4:29:16 PM #

Hey there

You might want to view the FireBug Net viewer, as you'll find the resource is downloaded for each component on the page.

We've found on one of our projects that a simple DateTime control was downloaded 6/7 times per page! and that WebResource contained the DLL, Javascript and all the images, so our page sizes were coming up to nearly 500KB per page load.

The .axd files are not cached either, so this may result in longer loading times at the expensive of developer convenience.

Regards,

Dominic

Dominic |

2/1/2008 10:08:23 AM #

Hi Brad,

Further to this post and your reply, I looked into the project I noticed this happening on and I was *gulp* incorrect with my comment above.

The browser is redirected to a specific resource via the querystring parameter. The WebResource.axd, then sends the required resource in the Response stream.

Thanks for clearing it up anyway, helped improve my understanding of the embedded resources.

Regards,

Dominic

Dominic |

9/4/2008 7:54:54 PM #

Can you use this for css files as well? thanks..

Kody Brown |

9/5/2008 9:08:32 AM #

Yes you can - any file can be embedded.

Brad |

9/30/2008 5:36:57 PM #

Hey, it works!! Thanks so much, for the very easy to understand directions.

Mike |

10/7/2008 3:06:08 AM #

This didn't work for me I created a test solution with only one project and tried to embed the image in that project to see if it worked and it didn't. The image src is "/WebResource.axd?d=VwfW8ErUfCRwpQgyYBrdYDolpP1et3F6ROzNeLFcxatM5mZaBYY0JgBQQgaPoUXN0&t=633589851245099191" style="border-width:0px;". Do you have a sample app?

Greg |

11/20/2008 1:12:20 AM #

Yes, demo app pls.

Salem |

3/3/2009 4:15:12 PM #

Looks like the casing must match on the resource, the entry in AssemblyInfo.cs and the code snippet where you reference the resource.

Satya |

Powered by BlogEngine.NET 1.5.0.7
Theme by Interakting

Interakting

A full service digital agency offering online strategy, design and usability, systems integration and online marketing services that deliver real business benefits and ensure your online objectives are met.

Calendar

<<  September 2010  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
27282930123
45678910

View posts in large calendar