What is the difference between attributes and filt

2019-03-24 12:58发布

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

Example:

SomeClassAttribute (or ISomeClassAttribute)

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.

3条回答
相关推荐>>
2楼-- · 2019-03-24 13:02

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

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

查看更多
疯言疯语
3楼-- · 2019-03-24 13:18

"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. Filters and Attributes are not exactly comparable concepts, they serve two different functions.

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

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 Authorize attribute. This attribute corresponds to an AuthorizationFilter that implements the IAuthorizationFilter interface. Applying the Authorize attribute 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 Attributes are 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.

查看更多
甜甜的少女心
4楼-- · 2019-03-24 13:28

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 question about attributes.

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 implements IAuthorizationFilter. 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:

void RegisterGlobalFilters(GlobalFilterCollection filters)
{
    filters.Add(new AuthorizeAttribute());
}
查看更多
登录 后发表回答