Asp.net mvc 停止MVC ViewMasterPage解析CSS URL

Asp.net mvc 停止MVC ViewMasterPage解析CSS URL,asp.net-mvc,master-pages,Asp.net Mvc,Master Pages,默认情况下,从url domain.com/urllevel1/urllevel2/访问的.NET MVC2中放置的/folderlevel1/folderlevel2/Site.Master母版页将解析此标记中的url: <link href="/Content/Site.css" rel="stylesheet" type="text/css" /> 到 这在我的multi-tennant MVC应用程序中变得有问题。我想阻止这种行为。我希望母版页不使用url。oscar 我相

默认情况下,从url domain.com/urllevel1/urllevel2/访问的.NET MVC2中放置的/folderlevel1/folderlevel2/Site.Master母版页将解析此标记中的url:

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

这在我的multi-tennant MVC应用程序中变得有问题。我想阻止这种行为。我希望母版页不使用url。

oscar

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

<link href="<%=Url.Content("~/Content/Site.css")%>" rel="stylesheet" type="text/css" />
当然,我可能错过了一些微妙的东西:

奥斯卡

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

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

当然,我可能遗漏了一些微妙的地方:

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

<head runat="server">

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

<head runat="server">
你可以用

<link href="<%=Url.Content("~/Content/Site.css")%>" rel="stylesheet" type="text/css" />
但这基本上总是转化为:

<link href="/Content/Site.css" rel="stylesheet" type="text/css" />
因此,您不妨使用后者。

您可以使用

<link href="<%=Url.Content("~/Content/Site.css")%>" rel="stylesheet" type="text/css" />
但这基本上总是转化为:

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

因此,您也可以使用后者。

我建议使用一种扩展方法让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;
        }
    }
}
然后确保将名称空间添加到视图文件夹中的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>

我建议使用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;
        }
    }
}
然后确保将名称空间添加到视图文件夹中的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>

就像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/默认情况下是您的内容文件夹,它的内容类似于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/默认是您的内容文件夹,它是内容

不幸的是,MVC已经为我做了这样的事情。我不想MVC改变链接,我想保持/Content/Site.css,如果母版页上是这么说的话。好的,奥斯卡,我现在完全理解你的要求了。嗯,也许删除首字母/“may”会产生不同的结果??不幸的是,MVC已经为我做了这样的事情。我不想MVC改变链接,我想保持/Content/Site.css,如果母版页上是这么说的话。好的,奥斯卡,我现在完全理解你的要求了。嗯,也许去掉首字母/“may”会产生不同的结果??我目前的解决方案有点不对劲:我还是不明白你的问题。你有多个母版页,每个母版页都应该链接到单独的CSS文件吗?我目前的解决方案有点麻烦:我还是不明白你的问题。您是否有多个母版页,每个母版页都应链接到单独的CSS文件?这有其他后果,应与此答案一起解释。这有其他后果,应与此答案一起解释。