Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET MVC Routing w/ Url Encoding Problems

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
like image 586
Luke Belbina Avatar asked Aug 29 '11 02:08

Luke Belbina


People also ask

What is the best way to URL encode a string?

In JavaScript, PHP, and ASP there are functions that can be used to URL encode a string. PHP has the rawurlencode() function, and ASP has the Server.URLEncode() function. In JavaScript you can use the encodeURIComponent() function. Click the "URL Encode" button to see how the JavaScript function encodes the text.

What is URL routing in MVC?

In MVC, routing is a process of mapping the browser request to the controller action and return response back. Each MVC application has default routing for the default HomeController. We can set custom routing for newly created controller.

How will you localize URL in MVC application?

To offer a website in multiple languages using ASP.Net we simply need to add some resource. resx files to our project and voilà. Based on the language of the browser, IIS will match the localization resource.

What makes it different a Web API routing from MVC routing?

If you are familiar with ASP.NET MVC, Web API routing is very similar to MVC routing. The main difference is that Web API uses the HTTP verb, not the URI path, to select the action. You can also use MVC-style routing in Web API.


2 Answers

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:

    public string ToUrlSafeBase64String(string Base64String)
    {
        // avoid any slashes, plus signs or equal signs
        // the following makes this base64 string url safe
        Base64String = Base64String.Replace("/", "_");
        Base64String = Base64String.Replace("+", "-");
        return Base64String.Replace("=", String.Empty);
    }

Then, use the following to re-create the original Base64 encoded string:

    public string FromUrlSafeBase64String(string Base64String)
    {
        // add back any slashes, plus signs or equal signs
        // the following makes this url safe string a base64 string
        Base64String = Base64String.Replace("_", "/");
        Base64String = Base64String.Replace( "-", "+");
        return Base64String.PadRight(Base64String.Length + (4 - Base64String.Length % 4) % 4, '=');
    }
like image 175
counsellorben Avatar answered Oct 08 '22 12:10

counsellorben


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:

routes.MapRoute(
            "email-validated",
            "email-validated",
            new { controller = "User", action = "EmailValidated" }
            );

Call it with a request like:

email-validated/?sessionId=XQiKC6KMM%2fmko4nOvzGRwPu9oaZFoCtXsFFJg3ZTf9S5rsBbLGjnz3FN3SJ0apEZcqK1PIcCY28mRMykB39XnFLKaL7390CDfLZiV77cso

And then change your function from

EmailValidatedFunction(string sessionId)
{
   //...do stuff with sessionId here
}

to:

EmailValidatedFunction()
{
   string sessionId = Request["sessionId"];
   //...do stuff with sessionId here
}
like image 23
mweber Avatar answered Oct 08 '22 12:10

mweber