Reference app relative virtual paths in .css file

后端 未结 8 2009
生来不讨喜
生来不讨喜 2020-12-24 02:03

Assume I have an \"images\" folder directory under the root of my application. How can I, from within a .css file, reference an image in this directory using an ASP.NET app

相关标签:
8条回答
  • 2020-12-24 03:07

    Unfortunately Firefox has a stupid bug here... the paths are relative to the path of the page, instead of being relative to the position of the CSS file. Which means if you have pages in different positions in the tree (like having Default.aspx in the root and Information.aspx in the View folder) there's no way to have working relative paths. (IE will correctly solve the paths relative to the location of the CSS file.)

    The only thing I could find is this comment on http://www.west-wind.com/weblog/posts/269.aspx but, to be honest, I haven't managed to make it work yet. If I do I'll edit this comment:

    re: Making sense of ASP.Net Paths by Russ Brooks February 25, 2006 @ 8:43 am

    No one fully answered Brant's question about the image paths inside the CSS file itself. I've got the answer. The question was, "How do we use application-relative image paths INSIDE the CSS file?" I have long been frustrated by this very problem too, so I just spent the last 3 hours working out a solution.

    The solution is to run your CSS files through the ASPX page handler, then use a small bit of server-side code in each of the paths to output the root application path. Ready?

    1. Add to web.config:
     <compilation debug="true">
     <!-- Run CSS files through the ASPX handler so we can write code in them. -->
     <buildProviders>
     <add extension=".css" type="System.Web.Compilation.PageBuildProvider" />
     </buildProviders>
     </compilation>
    
     <httpHandlers>
     <add path="*.css" verb="GET" type="System.Web.UI.PageHandlerFactory" validate="true" />
     </httpHandlers>
    
    1. Inside your CSS, use the Request.ApplicationPath property wherever a path exists, like this:

      #content { background: url(<%= Request.ApplicationPath %>/images/bg_content.gif) repeat-y; }

    2. .NET serves up ASPX pages with a MIME type of "text/html" by default, consequently, your new server-side CSS pages are served up with this MIME type which causes non-IE browsers to not read the CSS file correctly. We need to override this to be "text/css". Simply add this line as the first line of your CSS file:

      <%@ ContentType="text/css" %>
      
    0 讨论(0)
  • 2020-12-24 03:09

    I was having difficulty in getting background images to display for content containers and have tried many solutions similar to other posted here. I had set the relative path in the CSS file, set it as a style on the aspx page I wanted the background to display - nothing worked. I tried Marcel Popescu's solution and it still didn't work.

    I did end up getting it to work following a combination of Marcel's solution and trial and error. I inserted the code into the web.config, inserted the text/css line into my CSS file but I removed the background property in the CSS file altogether and set it as a style on the content container in the aspx page I wanted the background to display.

    It does mean that for each or any other pages that I want to display the background I will need to set the style background property but it works beautifully.

    0 讨论(0)
提交回复
热议问题