Red Taz
Red Taz

Reputation: 4179

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>

Upvotes: 6

Views: 1738

Answers (3)

Red Taz
Red Taz

Reputation: 4179

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.

Upvotes: 3

Robert
Robert

Reputation: 3302

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

Upvotes: 4

Farzin Zaker
Farzin Zaker

Reputation: 3606

Create a Resource Provider Aspx Page witch will check for a resource name in query string. then retrieves resource from dll and binary writes the resource in output.

then call it like this :

<script type="text/javascript" 
    src="ResourceProvider.aspx?name=My.Dll.Namespace.File.js"></script>

Upvotes: 0

Related Questions