The ApiResponseFilter is an action filter attribute that provides consistent structure to API responses. It wraps the API response body within an ApiResponse class.
You should install Eiffel.CrossCutting.ApiResponseConsistency NuGet package into your application.
Usage
To apply this filter, decorate your controllers or actions with [ApiResponseFilter].
For better experience decorate your methodProducesDefaultResponseTypeandProducesResponseTypeby using ApiResponse and ApiResponse<T> classes.
[ApiResponseFilter]publicclassBookingController:ControllerBase{ [HttpPost] [ProducesDefaultResponseType(typeof(ApiResponse))] [ProducesResponseType(StatusCodes.Status200OK, Type =typeof(ApiResponse<CreateBookingResponse>))]publicasyncTask<CreateBookingResponse> PostAsync([FromBody] CreateBookingRequest request) { // Your implementation details.. }}
To appy this filter to all controllers in your application, add to Mvc filters.
// Program.cs (.NET5 and above)publicstaticasyncTaskMain(string[] args){var builder =WebApplication.CreateBuilder(args); // Applies api response filter for all controllersbuilder.Services.AddControllers(options => {options.Filters.Add<ApiResponseFilter>(); });var app =builder.Build();app.Run();}
Disabling consistency for specific actions/controllers
If you don't want a particular action or controller to be wrapped with this consistency logic, decorate it with the DisableApiResponseConsistency attribute.
[DisableApiResponseConsistency]publicclassFileController:ControllerBase{ [HttpPost]publicasyncTaskPostAsync(IFormFile file) { // Your implementation details.. }}