How to use dependency injection in action filters in ASP.NET Core 3.1
Filters enable you to execute code at certain stages of the request processing pipeline. An action filter is a filter that is executed before or after the execution of an action method. By using action filters, you can make your action methods lean, clean, and maintainable.
There are several filters available for action methods, each of which corresponds to a different stage of the request processing pipeline. You can read more about action filters in my earlier article here. In this article we’ll discuss how we can work with dependency injection in action filters in ASP.NET Core 3.1.
To work with the code examples provided in this article, you should have Visual Studio 2019 installed in your system. If you don’t already have a copy, you can download Visual Studio 2019 here.
Create an ASP.NET Core 3.1 MVC project in Visual Studio 2019
First off, let’s create an ASP.Net Core project in Visual Studio 2019. Assuming Visual Studio 2019 is installed in your system, follow the steps outlined below to create a new ASP.Net Core project in Visual Studio.