views:

1097

answers:

3

I have a partial view called Login.ascx that has my login boxes that I include on a number of pages throughout my site. It works fine when the information is correct but I'm trying to do validation so if the login info is incorrect, I want to redirect the user back to the view they were in before to show them the login errors. What is the correct way of saying, return to the view you came from?

+1  A: 

Sounds like instead of asking how I do this, you should be asking yourself WHY am I doing it this way. Maybe it's a design decision rather than a technical question.

Though if you're really going to have one controller actions for multiple login pages you can try...

return Redirect(Request.UrlReferrer.ToString());

Or keep the route name in TempData and just use a RedirectToRoute(TempData["LoginRoute"]);

Both solutions have a bad code smell though.

Note that if you're not checking for cross-site injections that is just going to refer back to the other site. You may want to do some validation on the referring URL.

Chad Moran
A: 

For the built-in Login method of the AccountController there is a parameter named returnUrl which you can use like so:

  Return Redirect(returnUrl);

or

  Return RedirectToAction(returnUrl);

if you specify the returnUrl parameter as a valid actionlink.

I recently had similar problems - you might be able to find something here...

Tomas Lycken
Problem with this is, if you return because of a validation issue the returnUrl param is gone.
Chad Moran
+4  A: 

If a login fails from any page, I think I would direct them to a login view for the errors instead of the previous page. A dedicated login page is likely to have more UI space to display errors, etc. than a login control on another page. Having said that, you may want to include a returnUrl parameter to the Login action so that when the login is actually successful, the user is directed back to the place they were (or were attempting to get to).

tvanfosson