[关闭]
@liuruicai 2018-06-30T09:03:51.000000Z 字数 1434 阅读 537

Asp.net Core Filters

Learn


REF: Filters in ASP.NET Core

Filters run within the MVC action invocation pipeline, sometimes referred to as the filter pipeline. The filter pipeline runs after MVC selects the action to execute
how these filter types interact in the filter pipeline.

Filter scopes

A filter can be added to the pipeline at one of three scopes. You can add a filter to a particular action method or to a controller class by using an attribute. Or you can register a filter globally for all controllers and actions. Filters are added globally by adding it to the MvcOptions.Filters collection in ConfigureServices:

Built-in filter attributes

he framework includes built-in attribute-based filters that you can subclass and customize.
Attributes allow filters to accept arguments

Dependency injection

Filters can be added by type or by instance.
If you add an instance, that instance will be used for every request.
If you add a type, it will be type-activated, meaning an instance will be created for each request and any constructor dependencies will be populated by dependency injection (DI).

ServiceFilterAtriibute VS TypeFilterAttribute

A ServiceFilter retrieves an instance of the filter from DI.
ServiceFilterAttribute implements IFilterFactory. IFilterFactory exposes the CreateInstance method for creating an IFilterMetadata instance. The CreateInstance method loads the specified type from the services container (DI).
TypeFilterAttribute is similar to ServiceFilterAttribute, but its type isn't resolved directly from the DI container. It instantiates the type by using Microsoft.Extensions.DependencyInjection.ObjectFactory.

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注