【问题标题】:MVCContrib - Using Html.StyleSheet() Helper Renders Incorrect Path when used with T4MVCMVCContrib - 与 T4MVC 一起使用时,使用 Html.StyleSheet() 帮助程序呈现不正确的路径
【发布时间】:2010-07-21 15:01:08
【问题描述】:

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

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

渲染的路径错误计算为:

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

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

这是一个错误吗?

以下方法可以正常工作:

方法一:&lt;%=Html.Stylesheet("~/Content/Site.css")%&gt;

方法二:&lt;link type="text/css" rel="stylesheet" href="&lt;%:Links.Content.Site_css %&gt;" /&gt;

更新 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");
}

【问题讨论】:

  • Links.Content.Site_css 到底是什么?一个常数?一个领域?还有什么?它有什么价值?
  • 达林,请参阅我更新的问题以澄清 Links.Content.Site_css
  • Links.Content.Site_css 的实际值是多少?如果它以“~/”开头,则不会使用自动路径。如果您希望助手正常工作,它应该等于Site.css
  • Ryan 问的是 this 的评估结果,应该是“/Content/Site.css”。那么问题就变成了:为什么 Html.Stylesheet 不能处理这个?我不熟悉这个 API(但我拥有 T4MVC)。

标签: asp.net-mvc mvccontrib


【解决方案1】:

不是错误。这按预期工作。参考Using MvcContrib ScriptInclude, Stylesheet, And T4MVC

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

【讨论】:

  • 感谢您的回复。正如您所提到的,Html.ScriptInclude() 助手也会出现此问题。这似乎是 MVCContrib 和 T4MVC(MVCContrib 的一部分)之间的不匹配,在未来的版本中值得考虑。
猜你喜欢
  • 1970-01-01
  • 2017-06-09
  • 1970-01-01
  • 1970-01-01
  • 2017-01-12
  • 1970-01-01
  • 1970-01-01
  • 2018-01-18
  • 1970-01-01
相关资源
最近更新 更多