BeginRequest-like filter in MVC 3?

后端 未结 1 1528
囚心锁ツ
囚心锁ツ 2020-12-29 09:59

I have some code in my application that I need to execute on every request, before anything else executes (even before authentication). So far I\'ve been us

相关标签:
1条回答
  • 2020-12-29 10:26

    You could do this in the Initialize method of a base controller.

    Another possibility is to register a global filter:

    public class MyGlobalFilter : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            // that's gonna be hit
        }
    }
    

    and in the RegisterGlobalFilters event of your Global.asax:

    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        filters.Add(new HandleErrorAttribute());
        filters.Add(new MyGlobalFilter());
    }
    
    0 讨论(0)
提交回复
热议问题