Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redirecting to specified controller and action in asp.net mvc action filter

I have written an action filter which detects a new session and attempts to redirect the user to a page informing them that this has happened. The only problem is I can not figure out how to make it redirect to a controller/action combo in an action filter. I can instead only figure out how to redirect to a specified url. Is there a direct way to redirect to a controller/action combo in an action filter in mvc2?

like image 783
Nick Larsen Avatar asked Sep 29 '09 03:09

Nick Larsen


People also ask

How do I redirect an action filter?

If you want to use RedirectToAction : You could make a public RedirectToAction method on your controller (preferably on its base controller) that simply calls the protected RedirectToAction from System. Web. Mvc.

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.

How redirect to action in view in MVC?

You can use the RedirectToAction() method, then the action you redirect to can return a View. The easiest way to do this is: return RedirectToAction("Index", model); Then in your Index method, return the view you want.

What is difference between redirect and RedirectToAction?

RedirectToAction is meant for doing 302 redirects within your application and gives you an easier way to work with your route table. Redirect is meant for doing 302 redirects to everything else, specifically external URLs, but you can still redirect within your application, you just have to construct the URLs yourself.


2 Answers

Rather than getting a reference to HttpContent and redirecting directly in the ActionFilter you can set the Result of the filter context to be a RedirectToRouteResult. It's a bit cleaner and better for testing.

Like this:

public override void OnActionExecuting(ActionExecutingContext filterContext) {     if(something)     {         filterContext.Result = new RedirectToRouteResult(             new RouteValueDictionary {{ "Controller", "YourController" },                                       { "Action", "YourAction" } });     }      base.OnActionExecuting(filterContext); } 
like image 94
Richard Garside Avatar answered Oct 02 '22 05:10

Richard Garside


EDIT: The original question was about how to detect session logout, and then automatically redirect to a specified controller and action. The question proved much more useful as it's current form however.


I ended up using a combination of items to achieve this goal.

First is the session expire filter found here. Then I wanted someway to specify the controller/action combo to get a redirect URL, which I found plenty of examples of here. In the end I came up with this:

public class SessionExpireFilterAttribute : ActionFilterAttribute {     public String RedirectController { get; set; }     public String RedirectAction { get; set; }      public override void OnActionExecuting(ActionExecutingContext filterContext)     {         HttpContext ctx = HttpContext.Current;          if (ctx.Session != null)         {             if (ctx.Session.IsNewSession)             {                 string sessionCookie = ctx.Request.Headers["Cookie"];                 if ((null != sessionCookie) && (sessionCookie.IndexOf("ASP.NET_SessionId") >= 0))                 {                     UrlHelper helper = new UrlHelper(filterContext.RequestContext);                     String url = helper.Action(this.RedirectAction, this.RedirectController);                     ctx.Response.Redirect(url);                 }             }         }          base.OnActionExecuting(filterContext);     } } 
like image 26
Nick Larsen Avatar answered Oct 02 '22 04:10

Nick Larsen