URL 中 URL 编码的斜杠 [英] URL-encoded slash in URL

查看:88
本文介绍了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 然后路由再次匹配.

If I use the URL http://localhost:5000/Home/About/100%2f200 there is no matching route. I change the URL to http://localhost:5000/Home/About/100 then the route is matched again.

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

Is there any easy way to work with parameters that contain slashes? Other escaped values (space %20) seem to work.

对 Base64 进行编码对我有用.它使 URL 变得丑陋,但现在可以了.

To encode Base64 works for me. It makes the URL ugly, but that's OK for now.

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));
    }
}

编辑 1:

最后发现最好的方法是为我需要选择的每个项目保存一个格式良好的字符串.那好多了,因为现在我只编码值而从不解码它们.所有特殊字符都变成-".我的很多数据库表现在都有这个附加列URL".数据非常稳定,这就是我可以走这条路的原因.我什至可以检查URL"中的数据是否唯一.

At the end it turned out that the best way was to save a nicely formated string for each item I need to select. Thats much better because now I only encode values and never decode them. All special characters become "-". A lot of my db-tables now have this additional column "URL". The data is pretty stable, thats why I can go this way. I can even check, if the data in "URL" is unique.

编辑 2:

还要注意空格字符.它在 VS 集成网络服务器上看起来不错,但在 iis7 上有所不同 正确的 url 编码空格字符

Also watch out for space character. It looks ok on VS integrated webserver but is different on iis7 Properly url encode space character

推荐答案

如果它只是你的最后一个参数,你可以这样做:

If it's only your last parameter, you could do:

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

这篇关于URL 中 URL 编码的斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆