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编码空间字符
呼啦一阵风
倚天杖
牛魔王的故事
繁星点点滴滴
随时随地看视频慕课网APP
相关分类