2011-07-07 36 views

回答

4

如果您使用的是MVC 3,我推荐使用writing your ownactionfilter,然后您可以使用apply globally

一个小的代码示例:

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. 
    } 
} 

,然后在Global.asax.cs:

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

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

    RegisterGlobalFilters(GlobalFilters.Filters); 
    RegisterRoutes(RouteTable.Routes); 
} 
+0

看起来这正是我需要的。将尝试一下,并将其标记为答案 – Robylaz