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編碼空間字符
3 回答

倚天杖
TA貢獻1828條經驗 獲得超3個贊
如果它只是你的最后一個參數,你可以這樣做:
routes.MapRoute( "Default", // Route name "{controller}/{action}/{*id}", // URL with parameters new { controller = "Home", action = "Index", id = "" }); // Parameter defaults

牛魔王的故事
TA貢獻1830條經驗 獲得超3個贊
在.NET 4.0 beta 2中,CLR團隊提供了一種解決方法。
將其添加到您的web.config文件中:
<uri> <schemeSettings> <add name="http" genericUriParserOptions="DontUnescapePathDotsAndSlashes" /> </schemeSettings></uri>
這會導致Uri類根據描述URI的RFC行為,允許在路徑中轉義斜杠而不進行非轉義。出于安全原因,CLR團隊報告他們偏離了規范,并且在.config文件中設置它基本上使您獲得了所有其他安全性考慮因素的所有權,而不是取消斜杠。

繁星點點滴滴
TA貢獻1803條經驗 獲得超3個贊
以下是對解決方案的簡單解釋以及已經說過的內容的總結。
要求方:
UrlEncode您的路徑。
將'%'替換為'!'。
提出要求。
回應方:
更換 '!' 用'%'。
Url解碼您的路徑。
按預期使用參數。
沖洗,重復,享受。
- 3 回答
- 0 關注
- 2069 瀏覽
添加回答
舉報
0/150
提交
取消