Bens.Results
10.0.5
dotnet add package Bens.Results --version 10.0.5
NuGet\Install-Package Bens.Results -Version 10.0.5
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Bens.Results" Version="10.0.5" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Bens.Results" Version="10.0.5" />
<PackageReference Include="Bens.Results" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Bens.Results --version 10.0.5
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Bens.Results, 10.0.5"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Bens.Results@10.0.5
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Bens.Results&version=10.0.5
#tool nuget:?package=Bens.Results&version=10.0.5
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Bens.Results
An API result wrapper library for ASP.NET Core that provides a unified response format with built-in support for HTTP status codes and business error handling.
Status & Version
Features
- Unified Response Format - Consistent API responses with
Code,Title,Detail, and optionalData - Multiple Content Types - Built-in support for JSON and XML serialization
- HTTP Status Integration - Seamless integration with ASP.NET Core's
IResultandIActionResult - Type Safety - Generic and non-generic variants with full type support
- Validation Support - Model state validation error handling
- Modern C# - Leverages latest C# syntax and best practices
- Production Ready - Fully tested and optimized for performance
Installation
dotnet add package Bens.Results
Configuration
Register the services in your Program.cs:
using Bens.Results;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddApiResult();
var app = builder.Build();
app.Run();
Basic Usage
Simple Success Response
// Returns default success (Code: 0, Title: "ok")
return ApiResult.Ok();
// Returns success with data
return ApiResult.Ok(new { Name = "John", Email = "john@example.com" });
Error Responses
// Generic failure
return ApiResult.Fail("Operation failed");
// Failure with custom code and HTTP status
return ApiResult.Fail(1001, "Payment required", 402);
// HTTP status helpers
return ApiResult.BadRequest("Invalid input");
return ApiResult.Unauthorize("Authentication required");
return ApiResult.Forbid("Access denied");
return ApiResult.ServerError("Internal error occurred");
Validation Errors
public IActionResult CreateUser(CreateUserRequest request)
{
if (!ModelState.IsValid)
{
return ApiResult.BadRequest(ModelState);
}
// ... create user logic
return ApiResult.Ok(user);
}
Advanced Usage
Custom Response
public class ApiResult : ApiResultBase<ApiResult>
{
public IActionResult CustomResponse()
{
return new ApiResult
{
Code = 2000,
Title = "Custom message",
Detail = "Detailed description",
StatusCode = 200,
ContentType = "application/json"
};
}
}
XML Responses
// Create XML response
return ApiResult.Xml()
.WithStatusCode(200)
.WithCode(0)
.WithTitle("Success");
// XML response with data
return ApiResult.Xml<UserInfo>(userData);
Response Format
Success Response (JSON)
{
"code": 0,
"title": "ok",
"statusCode": 200,
"data": {
"id": 123,
"name": "John Doe"
}
}
Error Response (JSON)
{
"code": 400000,
"title": "One or more validation errors occurred.",
"statusCode": 400,
"detail": "Invalid request parameters",
"errors": {
"email": ["The Email field is required."],
"age": ["The field Age must be between 18 and 120."]
}
}
API Reference
Classes
| Class | Description |
|---|---|
ApiResult |
Non-generic API result for responses without data |
ApiResult<T> |
Generic API result for responses with typed data |
ApiResultProblemDetails |
Extended result for validation errors |
ApiListResult<T> |
Result for list data pagination |
Static Factory Methods
Success Methods
ApiResult.Ok()- Empty success responseApiResult.Ok<T>(T data)- Success with data
Failure Methods
ApiResult.Fail(string title, int? statusCode = null)- Generic failureApiResult.Fail(int code, string title, int statusCode = 200)- Failure with custom codeApiResult.Fail<T>(...)- Generic failure with data type
HTTP Status Helpers
ApiResult.BadRequest(string title, int code = -1)- 400 Bad RequestApiResult.BadRequest(ModelStateDictionary, string title, int code)- Validation errorsApiResult.Unauthorize(string title, int code = -1)- 401 UnauthorizedApiResult.Forbid(string title, int code = -1)- 403 ForbiddenApiResult.ServerError(string title, int code = -1)- 500 Internal Server Error
Factory Creators
ApiResult.New()- Create new empty resultApiResult.New<T>()- Create new generic resultApiResult.Json()- Create JSON result (default)ApiResult.Xml()- Create XML result
Interfaces
| Interface | Description |
|---|---|
IResult |
Base interface with Code, Title, Detail |
IResult<T> |
Generic interface with Data property |
IApiResult - Inherits IResult, IActionResult, IStatusCodeHttpResult, IContentTypeHttpResult |
|
IApiResult<T> |
Generic API result interface |
Extension Methods
WithXxx Methods
var result = new ApiResult<UserInfo>()
.WithCode(0)
.WithTitle("Success")
.WithDetail("Operation completed")
.WithStatusCode(200)
.WithContentType("application/json")
.WithData(userInfo);
License
This is part of the Bens framework.
Contributing
Contributions are welcome! Please ensure all tests pass before submitting a pull request.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net10.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.