Tuesday, January 24, 2023

Endpoint Execution filters in Minimal API (.NET 7)

Endpoint Execution filters are new Minimal API in .NET 7 and this feature allows developers to perform validations before the actual API request is executed. The developers can validate the input parameters (from the body/query string or URL template) and validate user authentication information as well. 

These validations will make the system more stable and secure as the input parameters are validated before executing the actual code. 

There are three types of Endpoint Execution filters are available.

  • Before Endpoint Execution Filter
  • After Endpoint Execution Filter
  • Short Circuit Execution Filter
To implement Endpoint execution filters, you need to use the IEndpointFilter interface and implement  
InvokeAsync(EndpointFilterInvocationContext context, EndpointFilterDelegate next ) with your custom logic code. You can access HTTPContext information by using  EndpointFilterInvocationContext context variable.

Before Endpoint Execution Filter:
As the name suggests, first the input parameters or the user authentication information are validated before executing the actual code. 


After Endpoint Execution Filter:
In this type of filter, the actual code is executed first and then the result of the execution will be used for further processing or transformation. 




Short-Circuit Execution Filter:
In this type of filter, the actual code will not be executed instead, the different logic will be performed and the response sent to the user.


The sample project can be downloaded from Github 

Happy Coding !!





No comments:

Post a Comment