How to use route handler filters in minimal APIs in ASP.NET Core 7 | InfoWorld

2022-10-01 10:07:25 By : Mr. YIFAN YIFAN

By Joydip Kanjilal , Columnist, InfoWorld |

ASP.NET Core 6 introduced a simplified hosting model that allows us to build lightweight APIs with minimal dependencies. Minimal APIs in ASP.NET Core 6 don’t use controllers, and they lack support for a number of useful ASP.NET features. One of these missing features is filters.

However, with ASP.NET Core 7 (now available in a release candidate), we can take advantage of the newly introduced IRouteHandlerFilter interface to incoporate filters in our minimal APIs. These filters can be used to modify the request or response objects as desired or to short-circuit the request processing pipeline.

This article discusses how we can work with route handler filters when building minimal API apps in ASP.NET Core 7. To use the code examples provided in this article, you should have Visual Studio 2022 Preview installed in your system. If you don’t already have a copy, you can download Visual Studio 2022 here.

First off, let’s create an ASP.NET Core 7 project in Visual Studio 2022 Preview. Follow these steps:

We’ll use this ASP.NET Core 7 Web API project to create a minimal API and implement route handler filters in the sections below.

Filters allow you to run code at certain stages of the request processing pipeline. In other words, a filter is a piece of code that is executed before or after an action method is executed. For example, you could use a filter to log every time someone accesses a web page or to validate the request parameters sent to an endpoint.

Filters offer a number of benefits:

You can take advantage of filters in minimal APIs to write code that can do the following:

You can take advantage of the IRouteHandlerFilter interface to modify the request or response or to short-circuit the request processing pipeline. You can also add cross-cutting concerns such as authentication, authorization, and logging. At a quick glance, here’s what you can achieve using this interface:

The following code snippet illustrates the IRoutehandler interface:

You can create a custom filter class via the IRouteHandlerFilter interface as shown in the code listing given below.

Next, you should register the filter as a service in the Program.cs file using the following code snippet.

It is possible to register a filter by using the RouteHandlerFilterDelegate or the AddFilter extension method. In this example, we’ll use the AddFilter extension method. Write the following code snippet in the Program.cs file.

You might often need to short-circuit the request processing pipeline. For example, let’s say you’ve built a microservices-based application and one of the services is not responding. In this case, all requests to this service would fail. Instead, you could short-circuit the request processing pipeline and fall back on some other service that is healthy.

Short-circuiting is often the desired action to avoid unnecessary processing. For example, if a request is generated for static files such as HTML, CSS, and JavaScript, you can write a filter that will intercept, handle, and serve that request while short-circuiting the rest of the pipeline. The short-circuiting stops the request processing pipeline and redirects the request to a fallback method or service.

Another example of short circuiting is the circuit breaker pattern in microservices-based applications. This pattern prevents an application from performing an operation that might fail. Typically, circuit breakers redirect requests to a fallback method if you encounter problems with a service or a service method. So, when an outage arises because of a failed service, you can use circuit breakers to redirect the request to fallback services or methods.

Here is how you can implement a custom short-circuit filter for your minimal API:

Filters allow you to run custom code before or after a certain point in the request processing pipeline. They also help you avoid the duplication of code across actions. The IRouteHandlerFilter Interface is a special filter that can be applied to an entire route or a single handler. It allows you to access the request object and then modify the request or response as required.

Joydip Kanjilal is a Microsoft MVP in ASP.Net, as well as a speaker and author of several books and articles. He has more than 20 years of experience in IT including more than 16 years in Microsoft .Net and related technologies.

Copyright © 2022 IDG Communications, Inc.

Copyright © 2022 IDG Communications, Inc.