Is there a clean way of accessing embedded resources (css/js/images etc) inside a dll.
For example, from an aspx page, can something similar to the below be used?
<script type="text/javascript" src="<%= ResolveUrl("~/My.Dll.Namespace.File.js") %>"></script>
A dynamic link library (DLL) is a collection of small programs that larger programs can load when needed to complete specific tasks. The small program, called a DLL file, contains instructions that help the larger program handle what may not be a core function of the original program.
Embedded resource has no predefined structure: it is just a named blob of bytes. So called “. resx” file is a special kind of embedded resource. It is a string-to-object dictionary that maps a name to an object. The object may be a string, an image, an icon, or a blob of bytes.
A resource DLL is characterized as follows: It contains the resource-specific code necessary to provide high availability for instances of one or more resource types. It exposes this code through a standard interface consisting of a set of entry point functions.
Open Solution Explorer add files you want to embed. Right click on the files then click on Properties . In Properties window and change Build Action to Embedded Resource . After that you should write the embedded resources to file in order to be able to run it.
I would suggest to take a look at WebResource.axd and the way how you can access embedded resources, like for example here:
http://weblogs.asp.net/jeff/archive/2005/07/18/419842.aspx
you can get the resource url on server side like this:
Page.ClientScript.GetWebResourceUrl(typeof(MyNameSpaces.MyControl), "MyNameSpaces.Resources.MyImage.gif")
and then render it on page
Thanks I had a look at WebResource a while back but didn't fully understand how it worked. Just had another look & I've now got a tidy little solution.
For those interested, I have a class in my dll called Resource with a static method as follows
public static string Get(Page p, string file) {
return p.ClientScript.GetWebResourceUrl(typeof(Resource), typeof(Resource).Namespace + ".Resources." + file);
}
After using the register directive in my master page (or web.config) I can now do the following
<link href="<%= Resource.Get(this.Page, "Styles.reset.css") %>" rel="stylesheet" type="text/css" />
(reset.css resides within a folder called Styles in the dll, hence Styles.filename.css)
Important Notes:
I discovered that the first argument accepted by GetWebResourceUrl must be of a class within the dll project not a class within consuming website.
I also had tremendous difficulty determining the correct fully qualified name to use for the resource in the AssemblyInfo.cs file. I discovered that my assembly name was not the same as my default namespace. The default namespace should be used to form the 'resourceName' argument for GetWebResourceUrl.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With