Partial ASP.NET MVC Views and Redirection

I have a partial view called Login.ascx in which there are my input fields that I include on several pages on my site. It works fine when the information is correct, but I'm trying to do a check, so if the login information is incorrect, I want to redirect the user back to the view they were in before showing them login errors. What is the right way to say, return to the view you came from?

+1
source share
3 answers

It seems that instead of asking how I do it, you should ask yourself WHY I do it like this. Perhaps this is a design decision, not a technical issue.

Although, if you really have one controller action for multiple login pages, you can try ...

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

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

Both solutions have a bad code smell though.

Please note that if you do not check cross-site injections that will simply return to another site. You can do some validation on the reference URL.

+2
source

If a crash on any page is not possible, I think I will direct them to the login window for errors instead of the previous page. A dedicated login page is likely to have more user interface space to display errors, etc., than a control on another page. Having said that, you can include the returnUrl parameter in the "Logon" action, so that upon successful login, the user will be directed back to the place where they were (or tried to get).

+5
source

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

  Return Redirect(returnUrl); 

or

  Return RedirectToAction(returnUrl); 

if you specify the returnUrl parameter as a valid link for actions.

I recently had similar problems - you could find something here ...

0
source

Source: https://habr.com/ru/post/1397805/


All Articles