1

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 1

5

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);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Looks like this is what I need. Will try it out in a bit and mark this as an answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.