私の地図は:
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));
}
}
編集1:
最後に、最善の方法は、選択する必要のあるアイテムごとに適切にフォーマットされた文字列を保存することであることが判明しました。今は値をエンコードするだけで、デコードすることはないので、これははるかに優れています。すべての特殊文字は「-」になります。私のdb-tableの多くには、この追加の列「URL」があります。データはかなり安定しているので、私はこの方法で行くことができます。「URL」のデータが一意かどうかも確認できます。
EDIT2:
また、スペース文字にも注意してください。VS統合Webサーバーでは問題ないように見えますが、iis7では異なります。適切にURLエンコードされたスペース文字