
MB.ResponseResult Package 📦
A standardized API response library for ASP.NET Core, featuring multi-language support and seamless integration.
👅 Installation
dotnet add package MB.ResponseResult
🚀 Quick Start
- Register Services (Program.cs)
#Basic Usage#
builder.Services.AddResponseResultServices();
Or
#Multiple-Language Support#
builder.Services.AddResponseResultServices(options =>
{
options.IsMultipleLang = true; // Enable multi-language support
});
🎯 Response Breakdown
✅ Success Helpers (Built with ResponseSuccess)
Created() | 201 | ApiResponse.Created(product) |
Ok(data) | 200 | ApiResponse.Ok(user) |
NoContent() | 204 | ApiResponse.NoContent() |
❌ Error Helpers (Built with ResponseFail)
NotFound() | 404 | ApiResponse.NotFound("User not found") |
BadRequest() | 400 | ApiResponse.BadRequest(errors) |
ValidationError() | 422 | ApiResponse.ValidationError(errors) |
InternalServerError() | 500 | ApiResponse.InternalServerError() |
🔧 Core Methods
ResponseSuccess() | Custom | statusCode , message , data | ApiResponse.ResponseSuccess(HttpStatusCode.Accepted, "Processing", data) |
ResponseFail() | Custom | statusCode , message , errors , data | ApiResponse.ResponseFail(HttpStatusCode.TooManyRequests, "Rate limited", errors) |
🔑 Key Features
Standardized Format
All responses follow the same consistent structure:
{
"data": {},
"message": "Operation succeeded",
"statusCode": 200,
"status": true,
"errors": []
}
Seamless Conversion
Convert ApiResponse to IActionResult automatically using:
.AsResult()
for synchronous responses : ApiResponse.Ok().AsResult();
.AsResultAsync()
for asynchronous responses : ApiResponse.Ok().AsResultAsync();
🌍 Localization (Multi-Language Support)
Localization Ready
Message keys like "Ok"
or "NotFound"
can be localized by implementing IResultLocalizationService
.
1.Implement a IResultLocalizationService Localization Service
public class CustomLocalizationService : IResultLocalizationService
{
public string GetLocalizedMessage(string messageKey, string languageCode)
{
//Your own logic
}
public Task<string> GetLocalizedMessageAsync(string messageKey, string languageCode)
{
//Your own logic
}
}
2.Register the service in the program.cs
builder.Services.AddScoped<IResultLocalizationService, CustomLocalizer>();
This README provides a quick guide to integrate and use the package effectively. Expand the documentation for advanced scenarios. 😊