Summary of knowledge points using dependency injection in ASP. NET Core filter

  • 2021-11-10 09:23:38
  • OfStack

How to use constructor injection for filter ActionFilterAttribute?

1 custom filters can be used directly by identifying them in a characteristic way

[ContentFilter]

Because the constructor requires passing parameters when it is used, then we can use this

ServiceFilter

In ASP. NET Core, we can use ServiceFilter to fulfill this requirement.

ServiceFilter allows us to parse a service to which an IoC container has been added, so we need to register ContentFilter under 1.

services.AddScoped < ContentFilter > ();

Identify directly in the form of characteristics where necessary

[ServiceFilter(typeof(ContentFilter))]

Then I found that it can be instantiated and used

The above is all the knowledge points introduced this time. Thank you for your study and support for this site.


Related articles: