UrlHelper扩展方法调用编码未执行

发布于 2024-07-17 14:04:45 字数 991 浏览 5 评论 0原文

我为 ASP.NET MVC UrlHelper 创建了一个简单的扩展方法。 它不接受任何参数,因为它的工作是从配置中查找样式表文件的名称并返回样式表的 url。 扩展方法大致如下所示:

public static string SiteStylesheet(this UrlHelper urlHelper)
{
    var scriptFilename = UserInterfaceConfiguration.GetSection()
                             .Mvc.SiteStylesheet;
    return urlHelper.Content(string.Format("~/Assets/Scripts/{0}",
                                           scriptFilename));
}

我这样使用它:

<link href="<%= Url.SiteStylesheet() %>" rel="Stylesheet" type="text/css" />

但是,该方法不会被执行,并且呈现以下内容:

href="../Views/Shared/%3C%25=%20Url.SiteStylesheet()%20%25%3E"

正如您所看到的,扩展方法没有被执行,而是整个内容只是被编码。 如果我更改方法签名以接受参数:

public static string SiteStylesheet(this UrlHelper urlHelper, string dummy)

则执行扩展方法并且输出符合预期:

href="/Assets/Stylesheets/FluidCMS.css"

那么我的问题是这是设计使然还是 ASP.NET MVC Web 窗体视图引擎中的错误?

I created a simple extension method for the ASP.NET MVC UrlHelper. It takes no arguments as its job is to look up the name of a stylesheet file from the configuration and return a url to the stylesheet. The extension method looks roughly like this:

public static string SiteStylesheet(this UrlHelper urlHelper)
{
    var scriptFilename = UserInterfaceConfiguration.GetSection()
                             .Mvc.SiteStylesheet;
    return urlHelper.Content(string.Format("~/Assets/Scripts/{0}",
                                           scriptFilename));
}

And I use it like this:

<link href="<%= Url.SiteStylesheet() %>" rel="Stylesheet" type="text/css" />

The method does not get executed, however, and the following is rendered:

href="../Views/Shared/%3C%25=%20Url.SiteStylesheet()%20%25%3E"

As you can see the extension method is not executed, rather the entire thing is just encoded. If I change the method signature to accept a parameter:

public static string SiteStylesheet(this UrlHelper urlHelper, string dummy)

then the extension method is executed and the output is as expected:

href="/Assets/Stylesheets/FluidCMS.css"

So my question then is this by design or is this a bug in the ASP.NET MVC Web Form view engine?

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

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

发布评论

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

评论(3

南风起 2024-07-24 14:04:45

这个问题已经出现过很多次了。 问题的根源在于 标签具有 runat="server",这会导致解析器将标签视为服务器标签。

最简单的解决方法是从 head 标签中删除 runat="server" 。 您丢失的是使链接 URL 相对于当前页面的逻辑,但由于您无论如何都在使用助手,因此不需要这样做。

This issue has come up a number of times. The root of the issue is that the <head> tag has runat="server", which causes the parser to treat tags as server tags.

The simplest workaround is to just remove runat="server" from the head tag. What you lose is the logic that makes the link URL's relative to the current page, but since you're using your helper anyway, you have no need for this.

舟遥客 2024-07-24 14:04:45

当我遇到这个问题时,是因为我的扩展方法位于 web.config 中未指定的命名空间中。

<add namespace="Your.Extension.Method.Namespace"/>

它位于configuration\system.web\pages\namespaces下

When I had this problem, it was because my extension methods were in a namespace that wasn't specified in the web.config.

<add namespace="Your.Extension.Method.Namespace"/>

It goes under configuration\system.web\pages\namespaces

情痴 2024-07-24 14:04:45

我想你发现了一个错误!

我尝试了一下,发现这只发生在母版页的头部部分,并且只发生在 标记中(

问题显然是 de href 属性内的文本未正确解释为代码块。

这超出了 ASP.NET MVC 的范围。 我在经典 Web 窗体 ASP.NET 站点的母版页中进行了尝试,问题仍然存在。 这似乎是 Web 表单渲染引擎中的一个错误或类似的东西。

I think you found a bug!

I tried it and found this only happens in the head section of a Master Page and only in the <link> tags (<script> tags render fine).

The problem obviously is the text inside de href attribute is not correctly interpreted as a code nugget.

This goes beyond ASP.NET MVC. I tried it in a Master Page in a classic Web Form ASP.NET site and the problem persists. It seems to be a bug in the Web Form rendering engine or something like that.

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