How to use dependency injection in action filters in ASP.NET Core 3.1


Take advantage of dependency injection to make your action methods in ASP.NET Core lean, clean, and maintainable.


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. 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.

  1. Launch the Visual Studio IDE.
  2. Click on “Create new project.”
  3. In the “Create new project” window, select “ASP.NET Core Web Application” from the list of templates displayed.
  4. Click Next.
  5. In the “Configure your new project” window, specify the name and location for the new project.
  6. Optionally check the “Place solution and project in the same directory” check box, depending on your preferences.
  7. Click Create.
  8. In the “Create a New ASP.NET Core Web Application” window shown next, select .NET Core as the runtime and ASP.NET Core 3.1 (or later) from the drop-down list at the top.
  9. Select “Web Application (Model-View-Controller)” as the project template to create a new ASP.NET Core MVC application. 
  10. Ensure that the check boxes “Enable Docker Support” and “Configure for HTTPS” are unchecked as we won’t be using those features here.
  11. Ensure that Authentication is set to “No Authentication” as we won’t be using authentication either.
  12. Click Create.

Following these steps should create a new ASP.Net Core MVC project in Visual Studio 2019. We’ll use this project in the sections below to illustrate the use of dependency injection in action methods in ASP.NET Core 3.1.

Use the RequestServices.GetService method in ASP.NET Core 3.1

Although it’s not a recommended practice, an easy way to retrieve a service that has been added to the service collection is by using the RequestServices.GetService method. Assuming that you have added a service named CustomService to your services container, you can use the following code to retrieve the service instance.

public override void OnActionExecuting(ActionExecutingContext context)
        {
            var customService = context.HttpContext.RequestServices.
            GetService(typeof(CustomService));
            //Write your code here
            _logger.LogWarning("Inside OnActionExecuting method...");
            base.OnActionExecuting(context);
        }

Use the ServiceFilter attribute in ASP.NET Core 3.1

A better practice is to take advantage of the ServiceFilter attribute — it can be applied at the controller or the action level. Here is the syntax for using this attribute.

Hire asp.net development company

Comments