Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embedded dll resources

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>

like image 674
Red Taz Avatar asked Apr 12 '11 09:04

Red Taz


People also ask

What is DLL in embedded?

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.

What is an embedded resource?

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.

What is a resource DLL?

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.

How do I add embedded resources to Visual Studio?

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.


2 Answers

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

like image 135
Robert Avatar answered Oct 24 '22 03:10

Robert


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.

like image 22
Red Taz Avatar answered Oct 24 '22 02:10

Red Taz