URL中的URL编码斜杠

URL中的URL编码斜杠

我的地图是:

routes.MapRoute(
   "Default",                                             // Route name
   "{controller}/{action}/{id}",                          // URL with params
   new { controller = "Home", action = "Index", id = "" } // Param defaults);

如果我使用URL http://localhost:5000/Home/About/100%2f200,则没有匹配的路由。我将URL更改为http://localhost:5000/Home/About/100然后再次匹配路由。

有没有简单的方法来处理包含斜杠的参数?其他转义值(空格%20)似乎有效。

编辑:

编码Base64对我有用。它使URL变得丑陋,但现在还可以。

public class UrlEncoder{ 
    public string URLDecode(string  decode)
    {
        if (decode == null) return null;
        if (decode.StartsWith("="))
        {
            return FromBase64(decode.TrimStart('='));
        }
        else
        {
            return HttpUtility.UrlDecode( decode) ;
        }
    }

    public string UrlEncode(string encode)
    {
        if (encode == null) return null;
        string encoded = HttpUtility.PathEncode(encode);
        if (encoded.Replace("%20", "") == encode.Replace(" ", ""))
        {
            return encoded;
        }
        else
        {
            return "=" + ToBase64(encode);
        }
    }

    public string ToBase64(string encode)
    {
        Byte[] btByteArray = null;
        UTF8Encoding encoding = new UTF8Encoding();
        btByteArray = encoding.GetBytes(encode);
        string sResult = System.Convert.ToBase64String(btByteArray, 0, btByteArray.Length);
        sResult = sResult.Replace("+", "-").Replace("/", "_");
        return sResult;
    }

    public string FromBase64(string decode)
    {
        decode = decode.Replace("-", "+").Replace("_", "/");
        UTF8Encoding encoding = new UTF8Encoding();
        return encoding.GetString(Convert.FromBase64String(decode));
    }}

EDIT1:

最后,结果证明最好的方法是为我需要选择的每个项目保存一个格式良好的字符串。这更好,因为现在我只编码值,从不解码它们。所有特殊字符都变为“ - ”。我的很多db-table现在都有这个额外的列“URL”。数据非常稳定,这就是我可以这样做的原因。如果“URL”中的数据是唯一的,我甚至可以检查。

EDIT2:

还要注意空间特征。它在VS集成网络服务器上看起来不错,但在iis7上是不同的url编码空间字符


呼啦一阵风
浏览 1992回答 3
3回答

倚天杖

如果它只是你的最后一个参数,你可以这样做:routes.MapRoute(     "Default",                                                // Route name     "{controller}/{action}/{*id}",                            // URL with parameters     new { controller = "Home", action = "Index", id = "" });  // Parameter defaults

牛魔王的故事

在.NET 4.0 beta 2中,CLR团队提供了一种解决方法。将其添加到您的web.config文件中:<uri>&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;<schemeSettings> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<add&nbsp;name="http"&nbsp;genericUriParserOptions="DontUnescapePathDotsAndSlashes"&nbsp;/> &nbsp;&nbsp;&nbsp;&nbsp;</schemeSettings></uri>这会导致Uri类根据描述URI的RFC行为,允许在路径中转义斜杠而不进行非转义。出于安全原因,CLR团队报告他们偏离了规范,并且在.config文件中设置它基本上使您获得了所有其他安全性考虑因素的所有权,而不是取消斜杠。

繁星点点滴滴

以下是对解决方案的简单解释以及已经说过的内容的总结。要求方:UrlEncode您的路径。将'%'替换为'!'。提出要求。回应方:更换 '!'&nbsp;用'%'。Url解码您的路径。按预期使用参数。冲洗,重复,享受。
打开App,查看更多内容
随时随地看视频慕课网APP