Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

How to embed resources in ASP.NET 2.0 assemblies


View products that this article applies to.

Introduction

Hi, this is Karthik with the Microsoft ASP.NET developer support team here at Microsoft. I have been working with ASP.NET for the past year and a half and have been involved with software development for about eight years now. The concept of embedding resources in assemblies that I discuss in this article is a pretty cool one. This could be very useful in large Web applications that involve lots of reusable components.

In this article, I will talk about and provide step-by-step instructions for creating and using embedded resources.

↑ Back to the top


What are these resources?

These resources could be any resources that you need for proper display, functioning, validation, and execution of the components in your project. These are vital resources that tend to and need to stay consistent across the application.

What are the advantages of embedding them?

You could put all your dependencies into one single assembly and then ship the assembly out to whoever needs it without having to worry about stuff like does the user has the latest client-side scripts? Did the user remember to put the images in the /something/something/images folder? Did the user set the permissions for the new folder accordingly? Is there any conflict between the resources that my library requires and any other library? Well, the list could go on.

Embedding the resources in an assembly

To do this, follow these steps:
  1. Add the resource as an existing item into the project.
  2. Set the resource type to be "embedded resource".

    Note This option is not available if you add the item directly to the Web site itself. Here is what you would see in such a situation:



    You can only apply this option on resources that are included with class libraries (assemblies in their own right). Here is what you would see:

  3. Next, open the AssemblyInfo.cs file of that library, and then add the following line of code to it:
    [assembly: WebResource("WebControlLibrary1.1.JPG", "img/jpeg")]
  4. Add the following line of code and a reference to System.web.dll if missing:
    using System.Web.UI
    You need to use the namespace when you declare the resources as well as when you request the resources.
  5. In the page (or in the control) that needs these resources, use the Page.ClientScript.GetWebResourceUrl method to get them.

    For example, you might use the following methods:
    • To get an image that is used as an embedded resource, you use the following code example.
      Image img = new Image();
      
      img.ImageUrl = Page.ClientScript.GetWebResourceUrl(typeof(WebControlLibrary1.WebCustomControl1), @"WebControlLibrary1.1.JPG");
      
      
    • To add a style sheet to a page header, you use the following code example.
      string includeTemplate ="<link rel='stylesheet' text='text/css' href='{0}' />";
      
      string includeLocation = Page.ClientScript.GetWebResourceUrl(typeof(WebControlLibrary1.WebCustomControl1), "Assembly.styles.css");
      
      LiteralControl include = new LiteralControl(String.Format(includeTemplate, includeLocation)); HtmlControls.HtmlHead) Page.Header).Controls.Add(include);
      
      

↑ Back to the top


References

For more information about the ClientScriptManager class, visit the following Microsoft Developer Network (MSDN) Web site:Designing assemblies
The following MSDN Web site describes the factors you should consider when you design assemblies:Assemblies (.NET Framework Developer's Guide)
Assemblies are the building blocks of Microsoft .NET Framework applications. They form the fundamental unit of deployment, version control, reuse, activation scoping, and security permissions. For more information, visit the following MSDN Web site:As always, feel free to submit ideas on topics you want addressed in future columns or in the Knowledge Base using the Ask For It form.

↑ Back to the top


Properties

Retired KB Content Disclaimer
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.

↑ Back to the top


Keywords: KB910445, kbasp, kbhowto

↑ Back to the top

Article Info
Article ID : 910445
Revision : 4
Created on : 5/11/2007
Published on : 5/11/2007
Exists online : False
Views : 451