【问题标题】:Urlhelper.action encodes some special charactersUrlhelper.action 对一些特殊字符进行编码
【发布时间】:2017-12-31 04:12:00
【问题描述】:

我有一个 ASP.Net Core Web api。 我使用 IUrlHelper.Action 生成一个完全限定的 URL。 当我通过时

Østjylland - Copy%*

它会转义 Ø 和空格,但不会转义 *

%C3%98stjylland%20-%20copy*

根据文档

https://msdn.microsoft.com/en-us/library/system.web.mvc.urlhelper.action(v=vs.118).aspx?f=255&mspperror=-2147217396#Anchor_15

不应转义任何字符。为什么会这样?我真的必须使用 WebUtility 首先从 UrlHelper 解码 url,然后再使用 WebUtility 对其进行编码以转义所有特殊字符吗?

【问题讨论】:

  • here 的回答对我有用。希望这会有所帮助!

标签: 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 的概念应该相同。希望这会有所帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多