How to redirect program flow to a controller action. I would like to simulate MVC’s RedirectToAction(“ActionName”, “ControllerName”, route values) call inside Global.asax.cs. How can I do it?
1 Answer
If you're using MVC 3, I'd recommend writing your own actionfilter, which you can then apply globally.
A small code example:
public class HandleSessionTimeoutAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(FilterExecutingContext filterContext)
{
// Do whatever it is you want to do here.
// The controller and request contexts, along with a whole lot of other
// stuff, is available on the filter context.
}
}
And then in your Global.asax.cs:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
// Register global filter
GlobalFilters.Filters.Add(new HandleSessionTimeoutAttribute());
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}
1 Comment
Robylaz
Looks like this is what I need. Will try it out in a bit and mark this as an answer