例:
在开发中,〜/ Images / Test.gif的路径可能会解析为/MyApp/Images/Test.gif,而在生产中,它可能会解析为/Images/Test.gif(取决于应用程序的虚拟目录) 。我显然希望避免在环境之间修改.css文件。
我知道您可以使用Page.ResolveClientUrl在渲染时动态地将url注入到控件的Style集合中。我想避免这样做。
解决方法
我唯一可以找到的是这个评论http://www.west-wind.com/weblog/posts/269.aspx,但老实说,我还没有设法让它工作。如果我会编辑这个评论:
re: Making sense of ASP.Net Paths by
Russ Brooks February 25,2006 @ 8:43
amNo 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?
- 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>
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;
}.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:06001