在URL网址恩codeD斜线 [英] URL-encoded slash in URL

查看:143
本文介绍了在URL网址恩codeD斜线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的地图是:

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

如果我使用的URL 的http://本地主机:5000 /首页/ / 100%2f200 没有匹配的路由。
我将URL更改为的http://本地主机:5000 /首页/关于/ 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.

编辑:

要带code的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));
    }
}

EDIT1:

在最后事实证明,最好的办法就是保存每个我需要选择项目一个很好的格式化字符串。那要好得多,因为现在我只带code值永不德code他们。所有特殊字符变成 - 。我的很多DB-表,现在有这样的附加列URL。该数据是pretty稳定,这就是为什么我可以走这条路。我甚至可以检查,如果在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.

EDIT2:

同时还要注意空格字符。它看起来VS集成的网络服务器确定,但是IIS7 <不同href=\"http://stackoverflow.com/questions/1651711/properly-url-en$c$c-space-character\">http://stackoverflow.com/questions/1651711/properly-url-en$c$c-space-character

Also watch out for space character. It looks ok on VS integrated webserver but is different on iis7 http://stackoverflow.com/questions/1651711/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网址恩codeD斜线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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