Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ReturnUrl in ASP.NET MVC

Tags:

asp.net-mvc

I currently have a login link on my application that looks something like this:

<a href="/login?ReturnUrl=" + <%= Request.RawUrl %>>Login</a> 

I want to handle the POST command on the login page in the controller action below:

[AcceptVerbs(HttpVerbs.Post)] public ActionResult Login(string returnUrl) {     // Authenticate user      return Redirect(returnUrl); } 

The problem here is if the RawUrl is something with multiple url parameters like "somepage?param1=1&param2=2&param3=3", then the returnUrl that gets passed into the Login action is truncated after the first ampersand: "somepage?param1=1".

I've tried UrlEncoding the RawUrl but that seem to make any difference. It seems that the ASP.NET MVC framework here is UrlDecoding the url params before mapping them to the controller action parameters, which ends up stripping off the additional url parameters I want to see in my returnUrl parameter.

Is there any way around this? I know I could just use Request.Path and parse out the values I need, but I thought I'd see if there was a cleaner approach first.

like image 405
Kevin Pang Avatar asked Apr 22 '09 18:04

Kevin Pang


People also ask

What is MVC ReturnURL?

The Forms Authentication makes use of ReturnUrl parameter to redirect user to the requested page after Login in ASP.Net MVC.

What is the use of ReturnURL?

The whole purpose of the returnUrl is to automatically send the user back to the page they were trying to access before they were authenticated/authorized.

What is ReturnURL?

A return URL redirects users back to the originating page during a checkout flow.


Video Answer


1 Answers

You're probably encoding the links incorrectly. Yes, they do need to be encoded. Here is how we do it:

<a href="<%= Url.Action("Delete", "TimeRecord",      new RouteValueDictionary(new { id = timeRecord.AltId,      returnUrl=ViewContext.HttpContext.Request.Url.PathAndQuery }) ) %>"> 
like image 58
Craig Stuntz Avatar answered Sep 17 '22 15:09

Craig Stuntz