内置方法对从 Url.Action 返回的 url 中的 & 符号进行编码?

发布于 2024-09-03 10:29:46 字数 508 浏览 3 评论 0原文

我正在使用 Url.Action 在文档类型为 XHTML strict 的网站上生成带有两个查询参数的 URL。

Url.Action("ActionName", "ControllerName", new { paramA="1" paramB="2" })

生成:

/ControllerName/ActionName/?paramA=1&paramB=2

但我需要它来生成带有 & 符号转义的 url:

/ControllerName/ActionName/?paramA=1&paramB=2

Url.Action 返回带有 & 符号未转义的 URL 的事实破坏了我的 HTML 验证。我当前的解决方案是手动将 Url.Action 返回的 URL 中的 & 符号替换为转义的 & 符号。是否有内置或更好的解决方案来解决这个问题?

I am using Url.Action to generate a URL with two query parameters on a site that has a doctype of XHTML strict.

Url.Action("ActionName", "ControllerName", new { paramA="1" paramB="2" })

generates:

/ControllerName/ActionName/?paramA=1¶mB=2

but I need it to generate the url with the ampersand escaped:

/ControllerName/ActionName/?paramA=1&paramB=2

The fact that Url.Action is returning the URL with the ampersand not escaped breaks my HTML validation. My current solution is to just manually replace ampersand in the URL returned from Url.Action with an escaped ampersand. Is there a built in or better solution to this problem?

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

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

发布评论

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

评论(4

何时共饮酒 2024-09-10 10:29:46

这对我有用:

Html.Raw(Url.Action("ActionName", "ControllerName", new { paramA="1" paramB="2" }))

This worked for me:

Html.Raw(Url.Action("ActionName", "ControllerName", new { paramA="1" paramB="2" }))
巴黎夜雨 2024-09-10 10:29:46

您无法使用 Server.HtmlEncode()

string EncodedUrl = Server.HtmlEncode(Url.Action("Action", "Controller", new {paramA = "1", paramB = "2"}));

http://msdn.htm 的任何原因。 microsoft.com/en-us/library/w3te6wfz.aspx

Any reason you can't use Server.HtmlEncode()

string EncodedUrl = Server.HtmlEncode(Url.Action("Action", "Controller", new {paramA = "1", paramB = "2"}));

http://msdn.microsoft.com/en-us/library/w3te6wfz.aspx

一身软味 2024-09-10 10:29:46

我最终只是为 Url.Action 创建了名为 Url.ActionEncoded 的扩展。代码如下:

namespace System.Web.Mvc {
    public static class UrlHelperExtension {
        public static string ActionEncoded(this UrlHelper helper, StpLibrary.RouteObject customLinkObject) {
            return HttpUtility.HtmlEncode(helper.Action(customLinkObject.Action, customLinkObject.Controller, customLinkObject.Routes));
        }
        public static string ActionEncoded(this UrlHelper helper, string action) {
            return HttpUtility.HtmlEncode(helper.Action(action));
        }
        public static string ActionEncoded(this UrlHelper helper, string action, object routeValues) {
            return HttpUtility.HtmlEncode(helper.Action(action, routeValues));
        }
        public static string ActionEncoded(this UrlHelper helper, string action, string controller, object routeValues) {
            return HttpUtility.HtmlEncode(helper.Action(action, controller, routeValues));
        }
    }
}

I ended up just creating extensions for Url.Action called Url.ActionEncoded. The code is as follows:

namespace System.Web.Mvc {
    public static class UrlHelperExtension {
        public static string ActionEncoded(this UrlHelper helper, StpLibrary.RouteObject customLinkObject) {
            return HttpUtility.HtmlEncode(helper.Action(customLinkObject.Action, customLinkObject.Controller, customLinkObject.Routes));
        }
        public static string ActionEncoded(this UrlHelper helper, string action) {
            return HttpUtility.HtmlEncode(helper.Action(action));
        }
        public static string ActionEncoded(this UrlHelper helper, string action, object routeValues) {
            return HttpUtility.HtmlEncode(helper.Action(action, routeValues));
        }
        public static string ActionEncoded(this UrlHelper helper, string action, string controller, object routeValues) {
            return HttpUtility.HtmlEncode(helper.Action(action, controller, routeValues));
        }
    }
}
未蓝澄海的烟 2024-09-10 10:29:46

您似乎遇到了 URL 呈现问题,而不是 URL 生成问题。 URL 中可以包含未编码的 & 符号,并且有些地方可能正是您所需要的。不过,在这种情况下,您将其嵌入到的 HTML 需要对 & 符号和各种其他字符进行编码。

虽然辅助方法可以节省视图中的一些输入,但我总是发现在最后可能的时刻完成任何显示编码可以节省一些麻烦,因此我总是使用真正的字符串/URL,直到我需要对其进行操作为止以特定的输出格式使用。如果您将辅助扩展从 您的答案在 HtmlHelper 中,您将不会在实际需要 URL 之前对其进行过早编码。

将其原始放入 HTML 中:

// with MVC3 auto-encoding goodness
<%:Url.Action(...)%>
// old-school MVC
<%=Html.Encode(Url.Action(...))%>

要将其直接放入视图中的锚点/src 属性中,您可能可以使用 Html.Encode 或不太严格的选项:

<%=Html.AttributeEncode(Url.Action(...))%>

You appear to have an issue with the rendering of a URL, not the generation of the URL. A URL can have an unencoded ampersand in it and there are places that may be exactly what you need. The HTML you are embedding it into in this case, though, wants ampersands and various other characters encoded.

While a helper method would save some typing in views, I always find it saves headaches to have the any display encoding done at the last possible moment so I am always working with the genuine string/URL right up until the point I need it manipulated for use in a particular output format. If you put the helper extension from your answer in HtmlHelper, you will be less tempted to prematurely encode your URL before it is actually needed.

To put it raw in HTML:

// with MVC3 auto-encoding goodness
<%:Url.Action(...)%>
// old-school MVC
<%=Html.Encode(Url.Action(...))%>

To put it in an anchor/src attribute directly in a view, you could probably get away with Html.Encode or the less-strict option:

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