.css文件中的参考应用程序相对虚拟路径 [英] Reference app relative virtual paths in .css file

查看:216
本文介绍了.css文件中的参考应用程序相对虚拟路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我在我的应用程序的根目录下有一个images文件夹目录。我如何从.css文件中,使用ASP.NET应用程序相对路径引用此目录中的图像。

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 relative path.

示例:

开发时,〜/ Images / Test.gif 可能会解析为 /MyApp/Images/Test.gif ,而在生产环境中,它可能会解析为 /Images/Test.gif (取决于虚拟目录应用程序)。显然,我想避免在环境之间修改.css文件。

When in development, the path of ~/Images/Test.gif might resolve to /MyApp/Images/Test.gif while, in production, it might resolve to /Images/Test.gif (depending on the virtual directory for the application). I, obviously, want to avoid having to modify the .css file between environments.

我知道你可以使用Page.ResolveClientUrl将一个url注入到控件的Style集合中在渲染时间。我想避免这样做。

I know you can use Page.ResolveClientUrl to inject a url into a control's Style collection dynamically at render time. I would like to avoid doing this.

推荐答案

不幸的是,Firefox有一个愚蠢的错误...路径是相对于路径,而不是相对于CSS文件的位置。这意味着如果您在树中有不同位置的页面(例如在根目录中有Default.aspx,在View文件夹中有Information.aspx),则没有办法有相对路径。 (IE将正确解决相对于CSS文件位置的路径。)

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.)

我唯一可以找到的是 http://www.west-wind.com/weblog/posts/269.aspx 但是,老实说,我还没有设法使它的工作。如果我这样做,我将编辑此评论:

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:通过
了解ASP.Net路径Russ Brooks 2月25日,2006 @ 8:43
am

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

没有人完全回答Brant的问题
关于CSS
文件本身的图像路径。我有答案。
问题是,我们如何使用
应用程序相对映像路径
在CSS文件中?我长期以来一直是
这个问题也感到沮丧,
所以我只花了最后3小时
制定一个解决方案。

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.

解决方案是通过ASPX页面处理程序运行CSS文件
,然后

中使用一小部分服务器端代码来输出根
应用程序路径。准备好了吗?

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. 添加到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. 在你的CSS中,使用Request.ApplicationPath属性
    存在路径,如下所示:

  1. Inside your CSS, use the Request.ApplicationPath property wherever a path exists, like this:

#content {
background:url ;%= Request.ApplicationPath
%> / images / bg_c​​ontent.gif)repeat-y;
}

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

.NET会默认使用MIME类型为text / html的ASPX页面,
服务器端CSS
页面使用此MIME
类型,导致非IE浏览器
不能正确读取CSS文件。我们
需要将它重写为
text / css。只需将此行作为
添加到CSS文件的第一行:

.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" %>



这篇关于.css文件中的参考应用程序相对虚拟路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆