覆盖的动作是否从基本动作继承动作过滤器? [英] Do overridden actions inherit action filters from the base action?

查看:83
本文介绍了覆盖的动作是否从基本动作继承动作过滤器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的MVC应用程序中,我有一些通用的基本控制器,它们可以处理Create \ Edit \ Delete动作,并且无需对多个模型的代码(定义类)进行任何更改即可工作.有时,我需要更改某些特定模型的基本操作之一,然后使用override来实现.现在,问题是,被覆盖的操作是从基本版本继承操作过滤器还是丢失过滤器?

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.

我的直觉告诉我它们确实是从基本操作继承的(否则,MVC会抱怨仅发布操作会导致歧义操作),但是我不确定.

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.

推荐答案

ASP.NET MVC查找并尊重基本控制器类上的动作筛选器.该筛选器应用于控制器类本身. 请参阅此.

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.

如果基类具有标记为virtual/abstract的操作方法,则覆盖的操作方法将不会执行.

Overriden action methods will not execute if base class has action methods marked with virtual/abstract.

在基类的动作过滤器的定义上,您可以将Inherited的AttributeUsage指定为false,因此它不适用于派生类

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)]

这篇关于覆盖的动作是否从基本动作继承动作过滤器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆