AutoWrapper.Core 5.0.0-rc-04
AutoWrapper

Language: English | 中文
AutoWrapper is a simple, yet customizable global HTTP exception handler and response wrapper for ASP.NET Core APIs. It uses an ASP.NET Core middleware to intercept incoming HTTP requests and automatically wraps the responses for you by providing a consistent response format for both successful and error results. The goal is to let you focus on your business code specific requirements and let the wrapper automatically handle the HTTP response. This can speedup the development time when building your APIs while enforcing own standards for your HTTP responses.
Main features:
- Exception handling.
- A configurable middleware
optionsto configure the wrapper. ModelStatevalidation error handling (support bothData AnnotationandFluentValidation).- A configurable
APIexception. - A consistent response format for
ResultandErrors. - A detailed
Resultresponse. - A detailed
Errorresponse. - A configurable
HTTPStatusCodesand messages. - Add Logging support for
Request,ResponseandExceptions. - Add support for Problem Details exception format.
- Add support for ignoring action methods that don't need to be wrapped using
[AutoWrapIgnore]filter attribute.
Breaking changes
- This release supports .NET 5 and .NET 6
- Middleware has been renamed from
UseApiResponseAndExceptionWrappertoUseAutoWrapper. Make sure to update yourStartup.csto use the new name. ProblemDetailsis now the default exception formatUseApiProblemDetailsExceptionhas been renamed toDisableProblemDetailsException- Removed
Newtonsoft.Jsondependency and replaced it withSystem.Text.Json - Use the interface
IApiResponsemodel instead of the concreteApiResponsemodel for returning responses using the default format. This allows you to add your own properties that will wrapped within theResultproperty. AutoWrapIgnoreandRequestDataLogIgnoreattributes now leaves underAutoWrapper.Attributesnamespace. The implementation was changed from usingIActionFilterto useAttribute, eliminating all the request header logic.- The following options has been removed:
- ApiVersion
- ReferenceLoopHandling
- UseCustomSchema
ReferenceLoopHandlingandDefaultContractResolveraren't still supported in .NET 5 that's why handling reference loop andApiResponseproperty mappings will not be supported when targetting .NET 5. .NET Core 3.1 will still use Newtonsoft.Json and it's only supported by AutoWrapper <= v4.5.1
Installation
- Download and Install the latest
AutoWrapper.Corefrom NuGet or via CLI:
PM> Install-Package AutoWrapper.Core -Version 5.0.0-rc-04
- Declare the following namespace within
Startup.cs
using AutoWrapper;
- Register the
middlewarebelow within theConfigure()method ofStartup.cs"before" theUseRouting()middleware:
app.UseAutoWrapper();
That's simple! Here’s how the response is going to look like for the default ASP.NET Core API template “WeatherForecastController” API:
{
"message": "GET Request successful.",
"result": [
{
"date": "2019-09-16T23:37:51.5544349-05:00",
"temperatureC": 21,
"temperatureF": 69,
"summary": "Mild"
},
{
"date": "2019-09-17T23:37:51.554466-05:00",
"temperatureC": 28,
"temperatureF": 82,
"summary": "Cool"
},
{
"date": "2019-09-18T23:37:51.554467-05:00",
"temperatureC": 21,
"temperatureF": 69,
"summary": "Sweltering"
},
{
"date": "2019-09-19T23:37:51.5544676-05:00",
"temperatureC": 53,
"temperatureF": 127,
"summary": "Chilly"
},
{
"date": "2019-09-20T23:37:51.5544681-05:00",
"temperatureC": 22,
"temperatureF": 71,
"summary": "Bracing"
}
]
}
ASP.NET Core 3.x
For ASP.NET Core 3.x versions, see the documentation here: AutoWrapper v4.x
Unwrapping the Result from .NET Client
AutoWrapper.Server is simple library that enables you unwrap the Result property of the AutoWrapper's ApiResponse object in your C# .NET Client code. The goal is to deserialize the Result object directly to your matching Model without having you to create the ApiResponse schema.
For example:
[HttpGet]
public async Task<IEnumerable<PersonDTO>> Get()
{
var client = HttpClientFactory.Create();
var httpResponse = await client.GetAsync("https://localhost:5001/api/v1/people");
IEnumerable<PersonDTO> people = null;
if (httpResponse.IsSuccessStatusCode)
{
var jsonString = await httpResponse.Content.ReadAsStringAsync();
people = Unwrapper.Unwrap<IEnumerable<PersonDTO>>(jsonString);
}
return people;
}
For more information, see: AutoWrapper.Server
Samples
- AutoWrapper: Prettify Your ASP.NET Core APIs with Meaningful Responses
- AutoWrapper: Customizing the Default Response Output
- AutoWrapper Now Supports Problem Details For Your ASP.NET Core APIs
- AutoWrapper.Server: Sample Usage
Feedback and Give a Star! :star:
I’m pretty sure there are still lots of things to improve in this project. Try it out and let me know your thoughts.
Feel free to submit a ticket if you find bugs or request a new feature. Your valuable feedback is much appreciated to better improve this project. If you find this useful, please give it a star to show your support for this project.
Contributors
- Vincent Maverick Durano - Blog
- Huei Feng - Github Profile
- ITninja04 - Github Profile
- Rahmat Slamet - Github Profile
- abelfiore - Github Profile
Want to contribute? Please read the CONTRIBUTING docs here.
Release History
See: Release Log
License
This project is licensed under the MIT License - see the LICENSE.md file for details.
Donate
If you find this project useful — or just feeling generous, consider buying me a beer or a coffee. Cheers! :beers: :coffee:
| | |
| ------------- |:-------------:|
| |
|
Thank you!
No packages depend on AutoWrapper.Core.
| Version | Downloads | Last updated |
|---|---|---|
| 5.0.0-rc-04 | 21 | 11/30/2024 |
| 5.0.0-rc-03 | 20 | 11/10/2024 |
| 5.0.0-rc-02 | 14 | 11/30/2024 |
| 5.0.0-rc | 21 | 11/30/2024 |
| 4.5.1 | 19 | 11/30/2024 |
| 4.5.0 | 127 | 12/03/2021 |
| 4.4.0 | 27 | 11/30/2024 |
| 4.3.2 | 21 | 11/30/2024 |
| 4.3.1 | 20 | 11/30/2024 |
| 4.3.0 | 20 | 11/30/2024 |
| 4.2.2 | 19 | 11/13/2024 |
| 4.2.1 | 20 | 11/30/2024 |
| 4.2.0 | 17 | 11/30/2024 |
| 4.2.0-rc | 22 | 11/30/2024 |
| 4.1.0 | 16 | 11/30/2024 |
| 4.1.0-rc2 | 19 | 11/22/2024 |
| 4.1.0-rc | 18 | 11/30/2024 |
| 4.0.1 | 20 | 11/30/2024 |
| 4.0.0 | 18 | 11/30/2024 |
| 4.0.0-rc | 20 | 10/19/2024 |
| 3.0.0 | 20 | 11/30/2024 |
| 2.1.0 | 21 | 11/30/2024 |
| 2.0.2 | 20 | 11/30/2024 |
| 2.0.1 | 22 | 11/30/2024 |
| 2.0.0-rc2 | 16 | 11/30/2024 |
| 2.0.0-rc | 16 | 11/30/2024 |
| 1.2.0 | 19 | 11/30/2024 |
| 1.1.0 | 23 | 11/30/2024 |
| 1.0.0 | 20 | 11/12/2024 |
| 1.0.0-rc | 20 | 11/30/2024 |