问题
In my MVC app I have a few generic base controllers that handle Create\Edit\Delete actions and work without a single change in code (aside from defining the class) for multiple models. Occasionally I need to alter one of the basic actions for some particular model and I achieve that using override
. Now, the question is, do the overridden actions inherit action filters from their base version or do they lose those?
For I second I thought that I can check that quickly in my existing code, but then I realized that all the actions would work either way.
My gut is telling me that they do get inherited from the base action (otherwise MVC would complain about ambiguous actions in case of post only actions), but I'm not quite certain.
Since I couldn't find this question anywhere else (much less answer), I thought it is worth asking even if it's something obvious for most people.
回答1:
ASP.NET MVC finds and respects the action filters on a base controller class.The one which is applied on controller class itself. See this.
Overriden action methods will not execute if base class has action methods marked with virtual/abstract.
On the definition of your action filter of base class you can specify AttributeUsage with Inherited as false , so it does not apply on derived ones
[System.AttributeUsage(System.AttributeTargets.All,
AllowMultiple = false,
Inherited = false)]
来源:https://stackoverflow.com/questions/24577781/do-overridden-actions-inherit-action-filters-from-the-base-action