ASP.NET MVC URL自动分辨率CSS文件 [英] ASP.NET MVC URL auto-resolution in CSS files

查看:114
本文介绍了ASP.NET MVC URL自动分辨率CSS文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在正常的WebForms情况下,任何根相对URL(例如〜/文件夹/ file.txt的)的 CSS文件,如:

In normal WebForms scenario, any root-relative URLs (e.g. ~/folder/file.txt) inside CSS files such as:

.form { background-image: url(~/Content/Images/form_bg.gif); }

将自动运行时,如果我指定的得到解决。

will automatically get resolved during runtime if I specify

<head runat="server">

在引用页。

然而,这不再是发生在一个ASP.NET MVC Beta1的网站。

However, that is no longer happening on an ASP.NET MVC Beta1 website.

有没有一种方法,我可以让没有诉诸黑客或CSS-加载程序文件此功能?也许就像的HttpModules还是什么?

Is there a way I could enable this functionality without resorting to hacks or CSS-loader file? Like maybe HttpModules or something?

还是我没有正确desigining我的网站?什么是应该是一个好的设计?

Or am I not desigining my website correctly? What is supposed to be a good design?

由于原来的ASP.NET的WebForms已经有这个功能,我想preFER如果可以利用现有的功能。但是,我没有太多的线索。

Since original ASP.NET WebForms already has this feature, I'd prefer to utilize any existing functionality if possible. But I don't have much clue.

这个Web应用程序将在多个环境中的〜根文件夹可能不明显部署。

This web application will be deployed in several environments where the ~ root folder might not be obvious.


编辑:我的意思是在文件的内容的URL不是文件的URL本身

I mean the url in the file's CONTENT not the file's url itself.

推荐答案

我不会自动求根字符打扰。我知道你是想同一个解决方案,为工作在根目录部署之间有所不同,但CSS文档中你不应该使用相对路径的任何问题。在CSS文件(在您的示例图像URL)中的路径将永远是相对的,无论该加载CSS文件的任何页面的路径的CSS文件的位置。所以,如果您的图片在〜/内容/图片和样式表都在〜/内容/样式,你'永远都会在能够使用背景图像:网址(../图像/ form_bg.gif); 键,不管它会工作页面的加载样式表的位置

I would not bother with the auto-root-finding ~ character. I understand that you want the same solution to work where the root directory differs between deployments, but within the CSS document you shouldn't have any problems using relative paths. The paths in the CSS document (to the image URL in your example) will always be relative to the location of the CSS file regardless of the path of any page that loads that CSS file. So if your images are in ~/Content/Images and your stylesheets are in ~/Content/Stylesheets, you'll always be able to use background-image: url(../Images/form_bg.gif); and it will work regardless of the location of the page that loads the stylesheet.

是否有一个原因,这是行不通的?

Is there a reason this wouldn't work?

这篇关于ASP.NET MVC URL自动分辨率CSS文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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