Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC: What is the correct way to redirect to pages/actions in MVC?

I am fairly new to MVC but not sure exactly which Redirect... replaces the standard redirect used in WebForms is the standard Response.Redirect()

For instance, I need to redirect to other pages in a couple of scenarios:

  1. WHen the user logs out (Forms signout in Action) I want to redirect to a login page.
  2. In a Controller or base Controller event e.g. Initialize, I want to redirect to another page (AbsoluteRootUrl + Controller + Action)

It seems that multiple redirects get called in some cases which causes errors, something to do with the fact a page is already being redirected? How can cancel the current request and just redirect?

Update:

The answer to this question (System.Web.Mvc.Controller Initialize) indicates that Initialize should not be used and OnActionExecuting should be used?

Any comments on why Initialize should not be used or why OnAuthorization is not a better option?

More Info:

This blog post (http://blog.wekeroad.com/blog/aspnet-mvc-securing-your-controller-actions/) indicates that OnActionExecuting is useful for authentication (as indicated in the link above) I guess adding this to that event in the base Controller class is fine as every page runs an Action in MVC so shouldn't make much difference and having the ability to redirect should be easier. This does make sense, but it also seems to make sense to me that things could be done in an event before this event and makes we question what those events are for? Will be giving OnActionExecuting a go..

like image 959
Mark Redman Avatar asked Apr 20 '10 17:04

Mark Redman


People also ask

How redirect another action method in MVC?

To redirect the user to another action method from the controller action method, we can use RedirectToAction method. Above action method will simply redirect the user to Create action method.

Which of the following is the correct statement about the type of redirect in ASP.NET MVC?

RedirectToAction("actionName", "controllerName");

How do you redirect to action?

and after login, we put the data in a class Person object and we use RedirectToAction like this: return RedirectToAction("profile","person",new { personID = Person. personID}); It's working normally, but the parameter are shown in the URL.


1 Answers

1) When the user logs out (Forms signout in Action) I want to redirect to a login page.

public ActionResult Logout() {     //log out the user     return RedirectToAction("Login"); } 

2) In a Controller or base Controller event eg Initialze, I want to redirect to another page (AbsoluteRootUrl + Controller + Action)

Why would you want to redirect from a controller init?

the routing engine automatically handles requests that come in, if you mean you want to redirect from the index action on a controller simply do:

public ActionResult Index() {     return RedirectToAction("whateverAction", "whateverController"); } 
like image 51
Jimmy Avatar answered Oct 13 '22 19:10

Jimmy