views:

341

answers:

3

I am fairly new to MVC but not sure exactly which Redirect... replaces the standard redirect used in WebForms ie 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 eg 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 (http://stackoverflow.com/questions/1679405/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 shouldnt 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..

+2  A: 
RedirectToAction("actionName", "controllerName");

It has other overloads as well, please check up!

Also, If you are new and you are not using T4MVC, then I would recommend you to use it!

It gives you intellisence for actions,Controllers,views etc (no more magic strings)

Mahesh Velaga
This is good for number 1, but you dont have that available in number 2.
NickLarsen
Indeed, in case number 2, the RedirectToAction appears to be ignored?
Mark Redman
+1  A: 

1) To redirect to the login page / from the login page, don't use the Redirect() methods. Use FormsAuthentication.RedirectToLoginPage() and FormsAuthentication.RedirectFromLoginPage() !

2) You should just use RedirectToAction("action", "controller") in regular scenarios.. You want to redirect in side the Initialize method? Why? I don't see why would you ever want to do this, and in most cases you should review your approach imo.. If you want to do this for authentication this is DEFINITELY the wrong way (with very little chances foe an exception) Use the [Authorize] attribute on your controller or method instead :)

UPD: if you have some security checks in the Initialise method, and the user doesn't have access to this method, you can do a couple of things: a)

Response.StatusCode = 403;
Response.End();

This will send the user back to the login page. If you want to send him to a custom location, you can do something like this (cautios: pseudocode)

Response.Redirect(Url.Action("action", "controller"));

No need to specify the full url. This should be enough. If you completely insist on the full url:

Response.Redirect(new Uri(Request.Url, Url.Action("action", "controller")).ToString());
Artiom Chilaru
There is an authentication check but its more to do with checking a user agains a tenant in a multi-tenancy application and other security checks are done. If there is a breach, the user should be logged out and redirected somewhere (this could be a generic login, specific login, home page or other page)
Mark Redman
Well.. My answer seems to cover this, no? Unless I missed something, in which case I'll be happy to help )
Artiom Chilaru
The redirects dont seem to work from initialize at all, so having a rethink...add more info to question...
Mark Redman
Try Response.Redirect(url, true) or run a Response.End() after the redirect.. that should force a redirect "NOW" :)
Artiom Chilaru
@Artiom: thanks will try that too...
Mark Redman
setting the endResponse parameter to true in Response.Redirect(url, true) doesnt seem to work either. The page is copmletes its round trip, but the ViewData/Model Data is lost. Will try moving code to OnActionExecuting event.
Mark Redman
+2  A: 

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");
}
Jimmy
Might want to note that the first item is in the Account controller.
GalacticCowboy
I still think that using FormsAuthentication.RedirectToLoginPage() should be used instead of return RedirectToAction("Login");
Artiom Chilaru
The Controller initialize event is detecting subdomains for a multi-tenancy, detecting subdomain changes and checking user authentication based on this is also done.
Mark Redman
You might want to look into MVC Areas, a new feature to MVC 2, it allows you to split up your application into different segments which might help solve your use-case
Jimmy
@Artiom, the FormsAuthentication.RedirectToLoginPage() does not clear the users authentication session, why use that when that is obviously not the desired outcome?
Jimmy
My Logout code does a Session.Abandon() before redirecting, so this is covered.. But my comment was directed towards having a standard location for the link to the login page (web.config) instead of redirecting manually like this. I don't mean it's a bad thing to do, just seems like a more "correct" thing to do, no? )
Artiom Chilaru
In other standard asp.net apps RedirectToLoginPage works fine and I too call Session.Abandon() The current situation requires calling both a standard Login page, used by tenant users or an extranet/workspace login page, used by clients of tenants.
Mark Redman
@Jimmy: MVC Areas look VERY interesting!! I am not sure they will help in the current situation as we will use the same code for all tenants, but in terms of organisation I will certainly need to investigate...
Mark Redman
MVC Areas walkthrough here: http://msdn.microsoft.com/en-us/library/ee671793(v=VS.100).aspx
Mark Redman
@Jimmy: The real (and very basic issue) was calling the Redirects without using "return"...doe! Alos MVC Ares have also turned out to be great..
Mark Redman