views:

1158

answers:

3

I have a CSS file that is embedded in my assembly. I need to set a background image for certain elements using this CSS file, and the image needs to be an embedded resource also. Is this possible? Is there any way I can reliably do this?

I ran into the problem when putting an existing stylesheet into this dll then realized images weren't showing up. I don't know of any way to make it work though because I would need to know the URL to the embedded image.

Has anyone done anything like this?

A: 

Edit: Akash Kava's answer appears to do what we've been looking for.

I asked a similar question to this and did not find any answers. It does not seem to be possible, because the URL to the resource is generated at runtime by a cryptographic function involving the machine key of the server. If you can set the machine key on your target system(s), you can predict what the value will be in that environment, but that value is not portable to any other system unless you control the machine key there as well.

Rex M
So my best option is to put the images out there on a server somewhere and hard code a link to them? This totally defeats the purpose of embedding resources. *sigh* I figured this is what the answer would be though.
Max Schmeling
A: 

What about exposing the resources through a Web service? Such as in the CSS file, set background: url( getImage.aspx?image=newyork.jpg )?

maxwellb
This is a good idea except that the resource is embedded in our common library, not in a web app. So unfortunately this won't work for me in this situation.
Max Schmeling
Then, reference the common library from a new web app, which is solely responsible for pulling out embedded resources from the common library?
maxwellb
+10  A: 
<% = WebResource("image1.jpg") %>

You can use above statement inside your CSS file, and while you register your CSS with WebResourceAttribute, you can set "PerformSubstitution" to true

Default.css
body{
    background: <%=WebResource("xyz.jpg")%>
}



[assembly, WebResource("Default.css","text/css", PerformSubstitution=true)]
[assembly, WebResource("xyz.jpg","image/jpg")]
Akash Kava
worked perfectly! thank you very much
Max Schmeling
I would vote you up 100 times for this if I could :)
Gordon Tucker
I found when using background-image rather than background I needed to wrap the WebResource server tag with url('').E.g. background-image: url('<%=WebResource("xyz.jpg") %>');
Daniel Ballinger