Specifying and using a WebResource to access embedded resources

Adam Miller had a great post on this blog about returning an embedded resource in an aspx page. Since I have some experience with embedded resources, I wanted to share instructions to access embedded resources using the WebResource attribute.

If you specify the embedded resource as an web resource, you can access it using a special url.

In your AssemblyInfo.cs, add a using statement for System.Web.UI and a WebResource attribute for each embedded resource you need to access.

Substitute the embedded resource path that is returned from GetManifestResourceNames() for “Your.Resource.Path.gif” and substitute a type from the assembly containing the embedded resource for “ATypeInYourAssembly”

using System.Web.UI;
[assembly: WebResource("Your.Resource.Path.gif", "image/gif")]

Then use the link returned from Page.ClientScript.GetWebResourceUrl(typeof(ATypeInYourAssembly), “Your.Resource.Path.gif”) to reference the resource.

Improve Your Life
Improve Your Team
Improve Your Code
Software Projects

David Walker

David Walker is a Secure Software Consultant, a Certified Secure Software Lifecycle Professional (CSSLP), and a Professional Scrum Master. He believes in secure and reliable software and productive happy teams. He lives in Orlando with his lovely wife Lynn and his 2 dogs.