C# MVC中的属性和过滤器有什么区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18508435/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 12:25:46  来源:igfitidea点击:

What is the difference between attributes and filters in MVC

c#asp.net-mvcasp.net-mvc-4asp.net-web-apiaction-filter

提问by S.A.

Now can I please get a comparison not just a definition.

现在我可以得到一个比较而不仅仅是一个定义。

Example:

例子:

SomeClassAttribute (or ISomeClassAttribute)

VS

VS

SomeClassFilter (or ISomeClassFilter)

I have a feeling that they can be used the same way but generally speaking "an attribute is applied" and a "filter is the functionality they produce." So I could "add an attribute to a method (or class or whatever) to apply a filter.

我有一种感觉,它们可以以相同的方式使用,但一般来说“应用了一个属性”和“过滤器是它们产生的功能”。因此,我可以“向方法(或类或其他)添加属性以应用过滤器。

回答by asymptoticFault

"So I could "add an attribute to a method (or class or whatever) to apply a filter."

“所以我可以”向方法(或类或其他任何东西)添加属性以应用过滤器。”

You've got it in that sentence right there. Filtersand Attributesare not exactly comparable concepts, they serve two different functions.

你在这句话里就明白了。 过滤器属性并不是完全可比的概念,它们提供两种不同的功能。

I believe Filteringin MVC is very well covered in this MSDN article.

我相信MVC 中的过滤在这篇MSDN 文章中有很好的介绍。

Attributes(at least those that apply to the filters) mark what the filter is applied to, i.e. an action method or a controller. An example would be the Authorizeattribute. This attribute corresponds to an AuthorizationFilterthat implements the IAuthorizationFilterinterface. Applying the Authorizeattribute to an action method tells MVC to authorize a request targeting that action method, applying it to a controller tells MVC to authorize any request targeting an action method of the controller, or authorization can also be applied globally for all requests. Now I said before, at least those that apply to the filters, because Attributesare a concept and syntax of .NET and not just MVC. There are attributes for so many other things and are generally to provide additional information about the property, method, class, they are applied to.

属性(至少那些应用于过滤器的属性)标记过滤器应用于什么,即操作方法或控制器。一个例子是Authorize属性。此属性对应于实现该接口的AuthorizationFilterIAuthorizationFilter。将Authorize属性应用于操作方法告诉 MVC 授权针对该操作方法的请求,将其应用于控制器告诉 MVC 授权针对控制器的操作方法的任何请求,或者也可以对所有请求全局应用授权。现在我之前说过,至少那些适用于过滤器的,因为Attributes是 .NET 的概念和语法,而不仅仅是 MVC。有许多其他事物的属性,通常用于提供有关应用它们的属性、方法、类的附加信息。

回答by SteveChapman

Attributes are a feature of .NET, MVC Filters are implemented using that feature.

属性是 .NET 的一个特性,MVC 过滤器是使用该特性实现的。

For example, System.Web.Mvc.HandleErrorAttributederives from the BCL System.Attribute. Filters apply behaviour via hooks into the MVC pipeline (roughly speaking).

例如,System.Web.Mvc.HandleErrorAttribute派生自 BCL System.Attribute。过滤器通过钩子将行为应用到 MVC 管道中(粗略地说)。

回答by Dusklight

In most cases, the attributes are used to describe metadata about methods/classes/etc. For example, there's the Serializable attribute to indicate that a class can be serialized, TestClass attribute to mark a class as a test, and the Obsolete attribute to mark something as obsolete. Reflection is used to extract this information by a process that wants to use them. It's covered well in this questionabout attributes.

在大多数情况下,属性用于描述有关方法/类/等的元数据。例如,Serializable 属性表示一个类可以被序列化,TestClass 属性将一个类标记为测试,以及 Obsolete 属性将某些内容标记为过时。反射用于由想要使用它们的进程提取这些信息。在这个关于属性的问题中很好地涵盖了它。

The filter attributes in MVC, such as AuthorizeAttribute, conveys extra information similar to other attributes -- a controller method or class decorated by AuthorizeAttribute indicates that authorization is required when used by MVC. But unlike some other attributes, the filter attributes themselves contain logic to carry out the actual function -- the AuthorizeAttribute derives from Attribute(via FilterAttribute) and also implementsIAuthorizationFilter. When MVC finds a controller class decorated by AuthorizeAttribute, it will call AuthorizeAttribute.OnAuthorization() method in to carry out the authorization. Also, when you are specifying global filters, you add the attribute class itself to the filters list, which can be a little bit confusing, but that's how it works:

MVC 中的过滤器属性,例如AuthorizeAttribute,传达了与其他属性类似的额外信息——由 AuthorizeAttribute 修饰的控制器方法或类指示当被 MVC 使用时需要授权。但与其他一些属性不同,过滤器属性本身包含执行实际功能的逻辑——AuthorizeAttribute派生自Attribute(通过 FilterAttribute)并且还实现了IAuthorizationFilter. 当MVC发现有AuthorizeAttribute修饰的控制器类时,会调用AuthorizeAttribute.OnAuthorization()方法进行授权。此外,当您指定全局过滤器时,您将属性类本身添加到过滤器列表中,这可能有点令人困惑,但这就是它的工作原理:

void RegisterGlobalFilters(GlobalFilterCollection filters)
{
    filters.Add(new AuthorizeAttribute());
}