Kemenkeu.ApiWrapper.Core
1.0.0
dotnet add package Kemenkeu.ApiWrapper.Core --version 1.0.0
NuGet\Install-Package Kemenkeu.ApiWrapper.Core -Version 1.0.0
<PackageReference Include="Kemenkeu.ApiWrapper.Core" Version="1.0.0" />
paket add Kemenkeu.ApiWrapper.Core --version 1.0.0
#r "nuget: Kemenkeu.ApiWrapper.Core, 1.0.0"
// Install Kemenkeu.ApiWrapper.Core as a Cake Addin #addin nuget:?package=Kemenkeu.ApiWrapper.Core&version=1.0.0 // Install Kemenkeu.ApiWrapper.Core as a Cake Tool #tool nuget:?package=Kemenkeu.ApiWrapper.Core&version=1.0.0
Kemenkeu.ApiWrapper.Core
Kemenkeu.ApiWrapper.Core is a library designed to standardize API responses in .NET applications. It provides a consistent response format, error handling, pagination support, and integrates easily with ASP.NET Core.
Features
- Standardized API Response: Easily wrap your API responses in a consistent format.
- Custom Exception Handling: Automatically handle exceptions and return structured error messages.
- Pagination Support: Manage pagination metadata effectively.
- Global Filters: Automatically apply response formatting to all controllers without additional configuration.
Compatibility
This library supports multiple frameworks:
- .NET Core 3.1
- .NET 5.0
- .NET 6.0 and later
Installation
From NuGet
To install the Kemenkeu.ApiWrapper.Core library, use the following command in your terminal or package manager console:
dotnet add package Kemenkeu.ApiWrapper.Core
Quick Start Guide
1. Configure Services
Add the library to your Startup.cs
file in the ConfigureServices
method:
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddKemenkeuApiWrapper();
}
2. Implement Global Filters
To enable the global response wrapping, simply add the KemenkeuApiWrapper
middleware in the Configure
method:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
// Add the ApiWrapper middleware
app.UseKemenkeuApiWrapper();
}
3. Using the API Response Wrapper
You can now use the response wrapper in your controllers. Here's an example of a simple controller:
[ApiController]
[Route("api/[controller]")]
public class SampleController : ControllerBase
{
[HttpGet]
public IActionResult GetSampleData()
{
var sampleData = new { Id = 1, Name = "Sample" };
return Ok(sampleData); // Automatically wrapped in ApiResponse
}
[HttpGet("{id}")]
public IActionResult GetSampleData(int id)
{
if (id <= 0)
{
return BadRequest(new ApiResponse { IsError = true, StatusCode = 400, Errors = new[] { "Invalid ID" } });
}
// Fetch data logic here...
return Ok(sampleData); // Automatically wrapped in ApiResponse
}
}
4. Pagination Support
To use pagination, specify pagination metadata on your actions:
[HttpGet]
[PaginatedResponse] // Custom attribute to override response if necessary
public IActionResult GetPaginatedData(int pageNumber = 1, int pageSize = 10)
{
// Fetch paginated data logic here...
var data = GetData(pageNumber, pageSize);
return Ok(data); // Pagination metadata will be included in headers
}
Contributing
If you would like to contribute to Kemenkeu.ApiWrapper.Core, please fork the repository and submit a pull request. We welcome contributions, bug reports, and feature requests!
License
This project is licensed under the MIT License - see the LICENSE file for details.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 is compatible. net5.0-windows was computed. net6.0 is compatible. 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 is compatible. 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 is compatible. 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.Mvc.NewtonsoftJson (>= 3.1.32)
- Newtonsoft.Json (>= 13.0.3)
-
net5.0
- Microsoft.AspNetCore.Mvc.NewtonsoftJson (>= 5.0.17)
- Newtonsoft.Json (>= 13.0.3)
-
net6.0
- Microsoft.AspNetCore.Mvc.NewtonsoftJson (>= 6.0.35)
- Newtonsoft.Json (>= 13.0.3)
-
net7.0
- Microsoft.AspNetCore.Mvc.NewtonsoftJson (>= 7.0.20)
- Newtonsoft.Json (>= 13.0.3)
-
net8.0
- Microsoft.AspNetCore.Mvc.NewtonsoftJson (>= 8.0.10)
- Newtonsoft.Json (>= 13.0.3)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.0.0 | 76 | 11/8/2024 |