Yes, you can do it, but thatβs not how it works out of the box. I have done the following:
- Create a base controller class and inherit all your controllers.
- Create an action filter attribute and inherit it from FilterAttribute and IActionFilter
- Decorate the base controller class with a new action filter attribute
Here is an example of an action filter attribute:
public class SetCultureAttribute : FilterAttribute, IActionFilter { #region IActionFilter implementation public void OnActionExecuted(ActionExecutedContext filterContext) { //logic goes here } public void OnActionExecuting(ActionExecutingContext filterContext) { //or logic goes here } #endregion IActionFilter implementation }
Here is an example of a controller base class with this attribute:
[SetCulture] public class ControllerBase : Controller { ... }
Using this method, if controller classes are inherited from ControllerBase, then the SetCulture action filter will always be executed. I have a complete sample and a post on this on my blog if you want a little more detail.
Hope this helps!
Ian suttle
source share