ASP.NET MVC event that happens just before action is called?

前端 未结 3 1225
既然无缘
既然无缘 2021-02-06 12:23

I want to set the value of Thread.CurrentCulture based on some route data, but I can\'t find an event to hook to that fires after the routes are calculated and befo

3条回答
  •  死守一世寂寞
    2021-02-06 13:17

    You could write a custom action filter attribute:

    public class CustomFilterAttribute : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            // This method is executed before calling the action
            // and here you have access to the route data:
            var foo = filterContext.RouteData.Values["foo"];
            // TODO: use the foo route value to perform some action
    
            base.OnActionExecuting(filterContext);
        }
    }
    

    And then you could decorate your base controller with this custom attribute. And here's a blog post illustrating a sample implementation of such filter.

提交回复
热议问题