Tod*_*odd 7 asp.net-mvc routes urlencode
我注意到Stackoverflow登录/注销链接上的returnurl URL参数没有被转义,但是当我尝试将路径作为参数添加到路由时,它会被转义.
所以/ login?returnurl =/questions/ask shows/login?returnurl =%2fquestions%2fask,这有点难看.如何让它不能逃避returnurl值?
这是我在代码中所做的事情:
Html.ActionLink("Login", "Login", "Account", new { returnurl=Request.Path }, null)
Run Code Online (Sandbox Code Playgroud)
如何让它不能逃避returnurl值
怎么样?
var url = Url.Action("Login", "Account", new {returnurl = Request.Path});
var unEncodedUrl = HttpUtility.UrlDecode(url);
Response.Write("<a href='" + unEncodedUrl + "'>...</a>");
Run Code Online (Sandbox Code Playgroud)
确保这是你想要的,URL编码有其目的.
我理解有关编码发生的评论之一是有原因的;这只是一个例外,而不是规则。
这是我整理的,请问如何改进?
public static string ActionLinkNoEscape(this HtmlHelper html, string linkText, string actionName, string controllerName, object values, object htmlAttributes)
{
RouteValueDictionary routeValues = new RouteValueDictionary(values);
RouteValueDictionary htmlValues = new RouteValueDictionary(htmlAttributes);
UrlHelper urlHelper = new UrlHelper(html.ViewContext.RequestContext, RouteTable.Routes);
string url = urlHelper.Action(actionName, controllerName);
url += "?";
List<string> paramList = new List<string>();
foreach (KeyValuePair<string, object> pair in routeValues)
{
object value = pair.Value ?? "";
paramList.Add(String.Concat(pair.Key, "=", Convert.ToString(value, CultureInfo.InvariantCulture)));
}
url += String.Join("&", paramList.ToArray());
TagBuilder builder = new TagBuilder("a");
builder.InnerHtml = string.IsNullOrEmpty(linkText) ? "" : HttpUtility.HtmlEncode(linkText);
builder.MergeAttributes<string, object>(htmlValues);
builder.MergeAttribute("href", url);
return builder.ToString(TagRenderMode.Normal);
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
5759 次 |
最近记录: |