Ble*_*ger 8 asp.net-mvc asp.net-mvc-routing asp.net-mvc-2
我正在使用Url.Action在具有XHTML严格的doctype的网站上生成带有两个查询参数的URL.
Url.Action("ActionName", "ControllerName", new { paramA="1" paramB="2" })
Run Code Online (Sandbox Code Playgroud)
产生:
/ControllerName/ActionName/?paramA=1¶mB=2
Run Code Online (Sandbox Code Playgroud)
但是我需要它来生成带有&符号转义的url:
/ControllerName/ActionName/?paramA=1&paramB=2
Run Code Online (Sandbox Code Playgroud)
Url.Action返回带有&符号未转义的URL的事实会破坏我的HTML验证.我目前的解决方案是只使用转义的&符号手动替换Url.Action返回的URL中的&符号.是否有内置或更好的解决方案来解决这个问题?
这对我有用:
Html.Raw(Url.Action("ActionName", "ControllerName", new { paramA="1" paramB="2" }))
Run Code Online (Sandbox Code Playgroud)
我最终只是为 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));
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7618 次 |
| 最近记录: |