0

我正在使用最新版本的 MVCContrib 并尝试通过以下辅助方法包含样式表:

<%=Html.Stylesheet(Links.Content.Site_css)%>

渲染的路径被错误地计算为:

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

实际路径应该是:/Content/Site.css

这是一个错误吗?

以下方法可以正常工作:

方法一: <%=Html.Stylesheet("~/Content/Site.css")%>

方法二: <link type="text/css" rel="stylesheet" href="<%:Links.Content.Site_css %>" />

更新 1:

Links.Content.Site_css 表示使用 T4MVC 自动生成的静态字段

更新 2: 这是 T4MVC 生成的代码的样子......

namespace Links {

    ...snipped for brevity...

    [GeneratedCode("T4MVC", "2.0"), DebuggerNonUserCode]
    public static class Content {
        private const string URLPATH = "~/Content";
        public static string Url() { return T4MVCHelpers.ProcessVirtualPath(URLPATH); }
        public static string Url(string fileName) { return T4MVCHelpers.ProcessVirtualPath(URLPATH + "/" + fileName); }

        public static readonly string Site_css = Url("Site.css");
}
4

1 回答 1

2

不是错误。这按预期工作。请参阅使用 MvcContrib ScriptInclude、Stylesheet 和 T4MVC

T4MVC 链接的输出是已解析的相对路径(意味着它们不再具有“~”)。MVC Contrib Helpers 假设如果通过它的 URL 没有“~”,那么它将为脚本添加“~/Scripts/”或为样式添加“~/content/css/'”。看到我已将我的脚本、样式和图像移动到“~/Content”文件夹下,对 MVC Contrib Html Helpers 进行了一些更改,可以使这项工作......提供某种机制来定义前面的路径脚本和样式(如果没有“~”) 用另一个选项覆盖 Html 帮助程序以不预先添加任何路径信息 在确定是否应将路径添加到 URL 时,可能搜索“/”而不是“~”

于 2010-07-26T16:55:20.043 回答