With ASP.NET Core 7, we can take advantage of the newly introduced IEndpointFilter interface to create filters and attach them to the endpoints in our minimal APIs. These filters can be utilized to modify demand or response items or to short-circuit the request processing pipeline.This post talks about how we can work with endpoint filters when developing very little API applications in ASP.NET Core 7. To use the code examples supplied in this post, you need to have Visual Studio 2022 installed in your system. If you do not currently have a copy, you can download Visual Studio 2022 here. Create an ASP.NET Core 7 very little Web API job in Visual
Studio 2022 To begin with, let’s create an ASP.NET Core 7 job in Visual Studio 2022 Sneak Peek. Follow these actions: Launch the Visual Studio 2022 IDE. Click”Develop brand-new job. “In the”Produce brand-new job
you can utilize filters to: Reject demands that do not satisfy specific criteria. Develop recyclable functions and classes. Concentrate on business logic of your application instead of spending
time writing code for cross-cutting concerns. Carry out code prior to and after an endpoint handler. Log request and response metadata. Confirm a
- demand and demand criteria.
- An endpoint filter enables you to intercept an inbound request, change it, short-circuit it, and even consolidate your cross-cutting concerns in a single place. Typical examples of cross-cutting concerns that might be handled in a single
- class are authorization, validation, and exception handling. The IEndpointFilter interface in ASP.NET
Core 7 ASP.NET Core 7 introduces a brand-new user interface called IEndpointFilter that can be used to modify the demand, modify the action, or short-circuit the demand pipeline. The IEndpointFilter user interface can be used to include info to incoming requests before the endpoint processes them.The IEndpointFilter interface is specified in the Microsoft.AspNetCore.Http namespace as shown listed below. public interface IEndpointFilter Why ought to I utilize the IEndpointFilter interface?The IEndpointFilter user interface is utilized to add performance to an HTTP endpoint. It is a basic user interface with just one approach, called InvokeAsync, that can include customized reasoning to the request/response pipeline.You can utilize the IEndpointFilter user interface when you wish to customize the demand or response at a particular point in the pipeline. You can use it to put all of your cross-cutting parts, such as logging, authentication, authorization, and encryption, in one place, where you can preserve them without changing any other part of your application’s code that leverages these parts. Create an endpoint filter in ASP.NET Core 7 You can register a filter by using a delegate that accepts EndPointFilterInvocationContext as a parameter and returns an EndpointFilterDelegate. The EndPointFilterInvocationContext circumstances provides access to the HttpContext of the existing request.The following code snippet shows how you can produce an easy endpoint filter that returns some text. Keep in mind how HttpContext has been used to obtain the Request headers. string AuthorName(string author)=>$ “Call of author: author”; app.MapGet( “/ endpointfilterexample/ “, AuthorName). AddEndpointFilter(async (invocationContext, next)=> var httpContext=invocationContext.HttpContext; var requestHeaders =httpContext.Request.Headers; var author=invocationContext.GetArgument(0); return await next (invocationContext );
); When you carry out the preceeding piece of code, the demand headers will be displayed as shown in Figure 1 listed below. IDG Figure 1. ASP.NET Core 7 endpoint filters in action. Chain multiple endpoint filters together in ASP.NET Core 7 The following code demonstrates how several endpoint filters can be chained together in the default endpoint. app.MapGet (“/ “,()= > return”Demonstrating numerous filters chained together.”; ).
AddEndpointFilter (async(endpointFilterInvocationContext, next)=> ). AddEndpointFilter(async(endpointFilterInvocationContext, next)=> ). AddEndpointFilter
(async (endpointFilterInvocationContext, next )=>
); When you perform the endpoint,
the three endpoint filters will be executed one after another.Create a customized filter in a very little API in ASP.NET Core 7 You can likewise produce custom filters by implementing the IEndpointFilter interface as displayed in the code bit provided below. public class MyCustomFilter: IEndpointFilter app.MapGet (“/ demo/ city”, ()=> return”Carry out filters in a chain. “;). AddEndpointFilter( ); To access the parameters associated with a specific HTTP demand, you can use the GetArguments approach of the EndpointFilterInvocationContext things. If you have numerous filters, you can register them utilizing the following code in your Program.cs