I have the following routing code:
routes.MapRoute(
"email-validated/{sessionId}",
"email-validated/{sessionId}",
new { controller = "User", action = "EmailValidated", sessionId = UrlParameter.Optional }
);
When I hit the route w/ something that is url encoded it won't match the route for %2f, %2b and some other escaped characters. It also won't match for non url encoded (things w/ + etc) For instance
This works:
email-validated/XQiKC6KMM%2cmko4nOvzGRwPu9oaZFoCtXsFFJg3ZTf9S5rsBbLGjnz3FN3SJ0apEZcqK1PIcCY28mRMykB39XnFLKaL7390CDfLZiV77cso
This doesn't work ( containts %2f etc):
email-validated/XQiKC6KMM%2fmko4nOvzGRwPu9oaZFoCtXsFFJg3ZTf9S5rsBbLGjnz3FN3SJ0apEZcqK1PIcCY28mRMykB39XnFLKaL7390CDfLZiV77cso
This doesn't work (contains + etc)
email-validated/XQiKC6KMM+mko4nOvzGRwPu9oaZFoCtXsFFJg3ZTf9S5rsBbLGjnz3FN3SJ0apEZcqK1PIcCY28mRMykB39XnFLKaL7390CDfLZiV77cso
you can use System.Web.HttpServerUtility.UrlTokenEncode (from http://brockallen.com/2014/10/17/base64url-encoding/#comments)
Here's the solution I've found to allow url encoded characters like the forward slash
%2f
in Asp.Net MVC urls.Add the following to the
Application_BeginRequest
method of yourGlobal.asax
file:Since this happens before MVC routing, you will be able to do what you originally wanted to do.
Just remember that you'll need to manually decode the parameters in your Action Methods to get their actual value (since we're preventing the framework from doing so).
It looks like the routing path handles escaped /'s and +'s weirdly. Try passing it in as a query string argument instead.
Make your endpoint:
Call it with a request like:
And then change your function from
to:
If you can, you need to make your sessionId URL safe. The sessionId is Base64 encoded, and there are three URL problem characters in Base64, "/", "+" and "=". Use the following to encode your sessionId when creating your link:
Then, use the following to re-create the original Base64 encoded string: