【问题标题】:Urlhelper.action encodes some special charactersUrlhelper.action 对一些特殊字符进行编码
【发布时间】:2017-12-31 04:12:00
【问题描述】:
【问题讨论】:
标签:
c#
url
asp.net-core
urlencode
asp.net-core-webapi
【解决方案1】:
here 的类似回答对我有用。
public static MvcHtmlString ActionNoEscape(this HtmlHelper html, string actionName, string controllerName, object values)
{
RouteValueDictionary routeValues = new RouteValueDictionary(values);
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());
return new MvcHtmlString(url);
}
当然,我在 .NET Framework 中工作,但 .NET Core 的概念应该相同。希望这会有所帮助!