内置方法EN code在URL中&符号从Url.Action回来? [英] Built in method to encode ampersands in urls returned from Url.Action?

查看:260
本文介绍了内置方法EN code在URL中&符号从Url.Action回来?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Url.Action来生成对具有XHTML严格的文档类型网站两个查询参数的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" })

生成:

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

但我需要它来生成URL的符号转义:

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

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

这是Url.Action返回与符号没有逃过我的休息HTML验证URL中的事实。我目前的解决办法是从Url.Action与逃脱符号返回的URL只是手动替换符号。是否有一个内置的或更好的办法解决这个问题?

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?

推荐答案

我最终只是创造扩展名为Url.Action Url.ActionEn codeD。在code是如下:

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));
        }
    }
}

这篇关于内置方法EN code在URL中&符号从Url.Action回来?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆