ARConsistency 1.1.0
See the version list below for details.
dotnet add package ARConsistency --version 1.1.0
NuGet\Install-Package ARConsistency -Version 1.1.0
<PackageReference Include="ARConsistency" Version="1.1.0" />
paket add ARConsistency --version 1.1.0
#r "nuget: ARConsistency, 1.1.0"
// Install ARConsistency as a Cake Addin #addin nuget:?package=ARConsistency&version=1.1.0 // Install ARConsistency as a Cake Tool #tool nuget:?package=ARConsistency&version=1.1.0
Read this document in different languages : Türkçe
ARConsistency
ARConsistency maintains response consistency by ensuring that all responses sent to the client for .Net Core Web Api projects are forwarded with the same template.
Setup
1. Install the Nuget Package
You can include this library in your project with the Nuget package.
Add the nuget package to your project using Package Manager Console or Nuget Package Manager.
PM> Install-Package ARConsistency
Or using .Net Cli
> dotnet add package ARConsistency
2. Settings
Add the following configuration to the "appsettings.json" file of your project.
"ApiConsistency": {
"IsDebug": true,
"ShowStatusCode": true,
"ShowApiVersion": false,
"ApiVersion": "1.0",
"IgnoreNullValue": true,
"UseCamelCaseNaming": true,
"EnableExceptionLogging": true
}
Note: If you wish, you can assign these settings manually in the next step without reading them from the config file. This step is optional.
3. Startup Implementation
Add the following into "Startup.cs".
public void ConfigureServices(IServiceCollection services)
{
// ...
services.AddControllers()
.AddApiResponseConsistency(config => Configuration.GetSection("ApiConsistency").Bind(config) );
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseApiResponseConsistency();
// ...
}
Sample #1
public IActionResult Get()
{
return Ok(_astronauts.FirstOrDefault());
}
Response
{
"statusCode": 200,
"isError": false,
"payload": {
"Id": 1,
"Name": "Neil",
"Surname": "Armstrong"
}
}
Sample #2
public IActionResult Get()
{
return new ApiResponse("Request successful.", _astronauts.FirstOrDefault(), 200, "2.0");
}
Response
{
"statusCode": 200,
"version": "2.0",
"message": "Request successful.",
"isError": false,
"payload": {
"Id": 1,
"Name": "Neil",
"Surname": "Armstrong"
}
}
Response Types
ARConsistency works with three types of responses of its own. The basic information of these classes are as follows.
Class Name | HTTP Status Code | Description |
---|---|---|
ApiResponse | 200 (OK) | It provides data return for successful response. |
ApiError | 400 (BadRequest) | Report any error to the client |
ApiException | 500 (Internal Server Error) | Throws errors and terminates the operation of the current procedure. |
In addition to these response types, ARConsistency also supports basic web api return types such as Ok(), BadRequest(). But Ok() return type contains only the data ApiResponse can contain information such as Message in addition to the returned data.
The HTTP Status Codes in the table above are default values and can be changed during use.
Logging
ARConsistency has the ability to log errors it captures with the ILogger interface. This setting (EnableExceptionLogging) is enabled by default unless you change it.
While this setting is on, errors occurring in the pipeline are transmitted to the logging mechanism via the ILogger interface and the summary of the error message is returned to the user as ApiExcepiton type api response. If the IsDebug option is on, the details of the error message are also included in the response.
When logging is turned off, error messages captured are thrown without processing.
Test Api Documentation
There is a .Net Core Web Api project in the repository where you can test the project. You can find the Postman documentation of the test api here.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp3.1 is compatible. |
-
.NETCoreApp 3.1
- Microsoft.AspNetCore.Http (>= 2.2.2)
- Microsoft.AspNetCore.Mvc.Core (>= 2.2.5)
- Microsoft.AspNetCore.Mvc.NewtonsoftJson (>= 3.1.1)
- Microsoft.Extensions.DependencyInjection (>= 3.1.1)
- Newtonsoft.Json (>= 12.0.2)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.