这是一个错误吗?或者它是 ASP.NET 4(或 MVC 2)中的设置?

发布于 2024-10-08 12:32:16 字数 1197 浏览 1 评论 0原文

我最近刚刚开始尝试 T4MVC,我喜欢消除魔术字符串的想法。

但是,当尝试在我的样式表的母版页上使用它时,我得到:

<link href="<%: Links.Content.site_css %>" rel="stylesheet" type="text/css" />

像这样渲染:

<link href="&lt;%: Links.Content.site_css %>" rel="stylesheet" type="text/css" />

而这些渲染正确:

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

看来,只要我在代码段内有双引号,它就可以工作。但是当我在那里放入其他任何东西时,它就逃脱了领先的“小于”。

这是我可以关闭的东西吗?这是一个错误吗?


编辑:

对于


编辑2:

最小情况:

<link href="<%: string.Empty %>" />

<link href="<%: "" %>" />


编辑3:

我有一个解决方法,我已经实现了一个HtmlHelper扩展,这样我就可以做到这一点:

<%: Html.StyleSheet(Links.Content.site_css) %>

我更喜欢对intellisens的支持,所以我实际上会坚持下去。现在,我只是想解决这个错误。

I just recently started trying out T4MVC and I like the idea of eliminating magic strings.

However, when trying to use it on my master page for my stylesheets, I get this:

<link href="<%: Links.Content.site_css %>" rel="stylesheet" type="text/css" />

rending like this:

<link href="<%: Links.Content.site_css %>" rel="stylesheet" type="text/css" />

Whereas these render correctly:

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

It appears that, as long as I have double quotes inside of the code segment, it works. But when I put anything else in there, it escapes the leading "less than".

Is this something I can turn off? Is this a bug?


Edit:

This does not happen for <script src="..." ... />, nor does it happen for <a href="...">.


Edit 2:

Minimal case:

<link href="<%: string.Empty %>" />

vs

<link href="<%: "" %>" />

Edit 3:

I have a workaround, I have implemented an HtmlHelper extension so that I can do this:

<%: Html.StyleSheet(Links.Content.site_css) %>

I like the support for intellisens better, so I'm actually going to stick with that. Right now, I'm just trying to solve the bug.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

属性 2024-10-15 12:32:17

我认为问题出在结肠上。尝试 而不是 .

I think the problem is the colon. Try <link href="<%= string.Empty %>" /> instead of <link href="<%: string.Empty %>" />.

十年不长 2024-10-15 12:32:17

根据 Gu, <%: 是 ASP.NET 4.0 的一项功能。尝试以 4.0 为目标,看看是否仍然失败。

According to the Gu, <%: is a feature of ASP.NET 4.0. Try targeting 4.0 and see if its still failing.

ι不睡觉的鱼゛ 2024-10-15 12:32:17

从 head 标记中删除 runat="server" 属性。

服务器生成的 head 标记将为任何链接标记呈现 HtmlLink 标记。 href的内容取自源码并经过urlencoded。对于脚本包含,则不执行此操作。

编辑:
如果您无法从 head 标记中删除 runat="server",请按如下方式编写链接标记:

<link href="<%= ... %>" runat="" />

以下给出错误“runat 属性必须具有值 server”

<link href="<%= ... %>myfile.css" runat="" />

Remove the runat="server" attribute from the head tag.

The server generated head tag will render a HtmlLink tag for any link tag. The content of href is taken from source and urlencoded. This is not done for script includes.

EDIT:
Write your link tags like following if you cant remove the runat="server" from head tag:

<link href="<%= ... %>" runat="" />

The following gives an error "runat attribute must have value server"

<link href="<%= ... %>myfile.css" runat="" />
月亮是我掰弯的 2024-10-15 12:32:16

对我来说,这看起来像是一个错误,编译后的输出是:

private global::System.Web.UI.HtmlControls.HtmlLink @__BuildControl__control5() {
    global::System.Web.UI.HtmlControls.HtmlLink @__ctrl;            
    @__ctrl = new global::System.Web.UI.HtmlControls.HtmlLink();
    @__ctrl.Href = "<%: String.Empty %>";
    ((System.Web.UI.IAttributeAccessor)(@__ctrl)).SetAttribute("rel", "stylesheet");
    ((System.Web.UI.IAttributeAccessor)(@__ctrl)).SetAttribute("type", "text/css");
    return @__ctrl;
}

这似乎仅当控件位于 head runat="server" 内时才会发生

It looks like a bug to me, the compiled output for that is:

private global::System.Web.UI.HtmlControls.HtmlLink @__BuildControl__control5() {
    global::System.Web.UI.HtmlControls.HtmlLink @__ctrl;            
    @__ctrl = new global::System.Web.UI.HtmlControls.HtmlLink();
    @__ctrl.Href = "<%: String.Empty %>";
    ((System.Web.UI.IAttributeAccessor)(@__ctrl)).SetAttribute("rel", "stylesheet");
    ((System.Web.UI.IAttributeAccessor)(@__ctrl)).SetAttribute("type", "text/css");
    return @__ctrl;
}

This seems to only happen when the control is inside a head runat="server"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文