停止 MVC ViewMasterPage 解析 CSS URL

发布于 2024-09-27 03:10:30 字数 430 浏览 2 评论 0原文

默认情况下,.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

今天小雨转甜 2024-10-04 03:10:30

您可能会遇到此问题,因为当您将 head 标记指定为服务器端控件时,ASP.NET 会执行一些魔术,如下所示:

<head runat="server">

这些技巧包括:

  • 解析相对 CSS 路径
  • 从视图填充标题和元标记@Page 指令

如果您不想要这些技巧,只需从 head 标记中删除 runat 属性即可:

<%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage" %>

<!DOCTYPE html>
<html>
<head> 
    <link href="Content/Site.css" rel="stylesheet" type="text/css" />
</head>
<body>
...
</body>
</html>

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:

<head runat="server">

These tricks include:

  • resolving relative CSS paths
  • populating title and meta tags from your view's @Page directive

If you don't want these tricks, simply remove the runat attribute from the head tag:

<%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage" %>

<!DOCTYPE html>
<html>
<head> 
    <link href="Content/Site.css" rel="stylesheet" type="text/css" />
</head>
<body>
...
</body>
</html>
天煞孤星 2024-10-04 03:10:30

你可以使用

<link href="<%=Url.Content("~/Content/Site.css")%>" rel="stylesheet" type="text/css" />

,但这基本上总是翻译成这样:

<link href="/Content/Site.css" rel="stylesheet" type="text/css" />

所以你不妨使用后者。

you can use

<link href="<%=Url.Content("~/Content/Site.css")%>" rel="stylesheet" type="text/css" />

but that basically always translates to this:

<link href="/Content/Site.css" rel="stylesheet" type="text/css" />

so you might as well just use the latter.

扎心 2024-10-04 03:10:30

正如 Kazi 的最佳实践条目 (http://weblogs.asp.net/rashid/archive/2009/04/03/asp-net-mvc-best-practices-part-2.aspx) 中提到的,访问资源时忽略路由。要做到这一点非常简单并且效果很好。将以下内容添加到 Global.asax 中的 AddRoutes 函数中

_routes.IgnoreRoute("assets/{*pathInfo}");

...其中“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

_routes.IgnoreRoute("assets/{*pathInfo}");

...where "assets/" is your content folder (by default it's "Content")

南笙 2024-10-04 03:10:30

奥斯卡,

我确信会有很多类似的答案,但标准方法是:

<link href="<%=Url.Content("~/Content/Site.css")%>" rel="stylesheet" type="text/css" />

当然,我可能错过了一些微妙的东西:)

oscar,

i'm sure there will be many similar answers to follow, but the standard way would be:

<link href="<%=Url.Content("~/Content/Site.css")%>" rel="stylesheet" type="text/css" />

I may have missed something subtle here of course :)

筱果果 2024-10-04 03:10:30

我建议使用 HtmlHelper 的扩展方法来为您处理此任务。

using System.Web;
using System.Web.Mvc;

namespace MyApplicationNamepsace.Views
{
    public static class HtmlExtensions
    {

        public static IHtmlString RelativeCssLink(this HtmlHelper helper, string fileNameAndRelativePath)
        {
            TagBuilder builder = new TagBuilder("link");
            builder.Attributes.Add("rel", "stylesheet");
            builder.Attributes.Add("type", "text/css");
            builder.Attributes.Add("href", fileNameAndRelativePath);

            IHtmlString output = new HtmlString(builder.ToString());
            return output;
        }
    }
}

然后确保将命名空间添加到views 文件夹中的web.config 文件中。

<system.web>
  <pages>
    <namespaces>
      <add namespace="MyApplicationNamespace.Views"/>
    </namespaces>
  </pages>
</system.web>

然后在您的母版页中使用它。

<head runat="server">
    <title><asp:ContentPlaceHolder ID="TitleContent" runat="server" /></title>
    <%: Html.RelativeCssLink("Content/Site.css") %>
</head>

I suggest using an extension method for the HtmlHelper to take care of this task for you.

using System.Web;
using System.Web.Mvc;

namespace MyApplicationNamepsace.Views
{
    public static class HtmlExtensions
    {

        public static IHtmlString RelativeCssLink(this HtmlHelper helper, string fileNameAndRelativePath)
        {
            TagBuilder builder = new TagBuilder("link");
            builder.Attributes.Add("rel", "stylesheet");
            builder.Attributes.Add("type", "text/css");
            builder.Attributes.Add("href", fileNameAndRelativePath);

            IHtmlString output = new HtmlString(builder.ToString());
            return output;
        }
    }
}

Then make sure you add the namespace to the web.config file in the views folder.

<system.web>
  <pages>
    <namespaces>
      <add namespace="MyApplicationNamespace.Views"/>
    </namespaces>
  </pages>
</system.web>

Then use it in your masterpage.

<head runat="server">
    <title><asp:ContentPlaceHolder ID="TitleContent" runat="server" /></title>
    <%: Html.RelativeCssLink("Content/Site.css") %>
</head>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文