Global.asax 中的 ASP.NET MVC 会话超时处理

1 asp.net-mvc asp.net-mvc-2

如何将程序流重定向到控制器操作。我想模拟 Global.asax.cs 内的 MVC\xe2\x80\x99s RedirectToAction(\xe2\x80\x9cActionName\xe2\x80\x9d, \xe2\x80\x9cControllerName\xe2\x80\x9d, 路由值) 调用。我该怎么做?

\n

Tom*_*han 5

如果您使用 MVC 3,我建议您编写自己的 actionfilter,然后您可以全局应用它

一个小代码示例:

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.
    }
}
Run Code Online (Sandbox Code Playgroud)

然后在你的Global.asax.cs:

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();

    // Register global filter
    GlobalFilters.Filters.Add(new HandleSessionTimeoutAttribute());

    RegisterGlobalFilters(GlobalFilters.Filters);
    RegisterRoutes(RouteTable.Routes);
}
Run Code Online (Sandbox Code Playgroud)