停止 MVC ViewMasterPage 解析 CSS URL
默认情况下,.NET MVC2 中的母版页按如下方式放置:从 url domain.com/urllevel1/urllevel2/
访问的 /folderlevel1/folderlevel2/Site.master
将解析以下 URL this 标签:
<link href="/Content/Site.css" rel="stylesheet" type="text/css" />
to
<link href="../../Content/Site.css" rel="stylesheet" type="text/css" />
这在我的多租户 MVC 应用程序中会出现问题。我想阻止这种行为。我希望母版页保留 url。
By default Master pages in .NET MVC2 placed like this /folderlevel1/folderlevel2/Site.master
accessed from the url domain.com/urllevel1/urllevel2/
will resolve the URL in this tag:
<link href="/Content/Site.css" rel="stylesheet" type="text/css" />
to
<link href="../../Content/Site.css" rel="stylesheet" type="text/css" />
This becomes problematic in my multi-tennant MVC app. And I want to stop this behaviour. I want the master page to leave the url alone.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可能会遇到此问题,因为当您将
head
标记指定为服务器端控件时,ASP.NET 会执行一些魔术,如下所示:这些技巧包括:
如果您不想要这些技巧,只需从
head
标记中删除runat
属性即可:You are probably having this issue because ASP.NET performs magic tricks when you specify the
head
tag as a server side control like so:These tricks include:
If you don't want these tricks, simply remove the
runat
attribute from thehead
tag:你可以使用
,但这基本上总是翻译成这样:
所以你不妨使用后者。
you can use
but that basically always translates to this:
so you might as well just use the latter.
正如 Kazi 的最佳实践条目 (http://weblogs.asp.net/rashid/archive/2009/04/03/asp-net-mvc-best-practices-part-2.aspx) 中提到的,访问资源时忽略路由。要做到这一点非常简单并且效果很好。将以下内容添加到 Global.asax 中的 AddRoutes 函数中
...其中“assets/”是您的内容文件夹(默认情况下为“Content”)
Like mentioned on Kazi's best practices entry (http://weblogs.asp.net/rashid/archive/2009/04/03/asp-net-mvc-best-practices-part-2.aspx), ignore routing when accessing resources. To do this it's very simple and works well. Add the below to your AddRoutes function in Global.asax
...where "assets/" is your content folder (by default it's "Content")
奥斯卡,
我确信会有很多类似的答案,但标准方法是:
当然,我可能错过了一些微妙的东西:)
oscar,
i'm sure there will be many similar answers to follow, but the standard way would be:
I may have missed something subtle here of course :)
我建议使用
HtmlHelper
的扩展方法来为您处理此任务。然后确保将命名空间添加到views 文件夹中的web.config 文件中。
然后在您的母版页中使用它。
I suggest using an extension method for the
HtmlHelper
to take care of this task for you.Then make sure you add the namespace to the web.config file in the views folder.
Then use it in your masterpage.