EnhancedApiResponse 1.0.2

dotnet add package EnhancedApiResponse --version 1.0.2
                    
NuGet\Install-Package EnhancedApiResponse -Version 1.0.2
                    
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="EnhancedApiResponse" Version="1.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="EnhancedApiResponse" Version="1.0.2" />
                    
Directory.Packages.props
<PackageReference Include="EnhancedApiResponse" />
                    
Project file
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 EnhancedApiResponse --version 1.0.2
                    
#r "nuget: EnhancedApiResponse, 1.0.2"
                    
#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 EnhancedApiResponse@1.0.2
                    
#: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=EnhancedApiResponse&version=1.0.2
                    
Install as a Cake Addin
#tool nuget:?package=EnhancedApiResponse&version=1.0.2
                    
Install as a Cake Tool

EnhancedApiResponse is a lightweight and flexible .NET package to simplify the handling of API responses. It provides standardized structures for successful and error responses, along with built-in pagination support and global exception handling.

Features

  • Standardized API response structure.
  • Pagination metadata for paginated endpoints.
  • Middleware for global exception handling.
  • Simplifies API response creation with reusable methods.

Installation

You can install the package via NuGet:

dotnet add package EnhancedApiResponse

Usage

1. API Response Factory

Use the ApiResponse class to generate consistent responses for your API endpoints.

Example: Successful Response
using EnhancedApiResponse;

[HttpGet("{id}")]
public IActionResult GetUser(int id)
{
    var user = _userService.GetUserById(id);
    if (user == null)
    {
        return NotFound(ApiResponse<string>.Error("User not found."));
    }

    return Ok(ApiResponse<object>.Success(user, "User retrieved successfully."));
}

Example: Error Response

using EnhancedApiResponse;

[HttpPost]
public IActionResult CreateUser(CreateUserRequest request)
{
    if (!ModelState.IsValid)
    {
        return BadRequest(ApiResponse<string>.Error("Invalid request data."));
    }

    return Ok(ApiResponse<object>.Success(newUser, "User created successfully."));
}

2. Pagination Support

Use the PaginationHelper class to add metadata for paginated responses.

using EnhancedApiResponse;

[HttpGet]
public IActionResult GetUsers(int page = 1, int pageSize = 10)
{
    var users = _userService.GetUsers(page, pageSize, out var totalRecords);
    var metadata = PaginationHelper.GenerateMetadata(page, pageSize, totalRecords);

    return Ok(ApiResponse<object>.Success(users, "Users retrieved successfully.", metadata));
}

3. Global Exception Handling

The ExceptionMiddleware simplifies error handling by catching unhandled exceptions and returning a standardized error response.

Register Middleware Add the middleware in Program.cs or Startup.cs:

using EnhancedApiResponse;

var app = builder.Build();

app.UseMiddleware<ExceptionMiddleware>();

app.Run();

Example Error Response

If an unhandled exception occurs, the middleware returns:

{
  "status": "Error",
  "message": "An unexpected error occurred.",
  "data": "Exception details here"
}

API Response Structure

The ApiResponse<T> class provides a consistent structure for all responses:

{
  "status": "Success",
  "message": "Operation completed successfully.",
  "data": { /* Your data */ },
  "metadata": { /* Optional metadata */ }
}

Properties

Property Type Description
Status string Response status: Success or Error
Message string A human-readable message
Data T The response payload
Metadata object Optional metadata (e.g., pagination)

Development

Prerequisites

  • .NET SDK 6.0 or higher
  • NuGet CLI for packaging ##Build the Project Clone the repository and build the solution:
git clone https://github.com/khannouman645/EnhancedApiResponse.git
cd EnhancedApiResponse
dotnet build

Contributing

Contributions are welcome! Here's how you can contribute:

  • Fork the repository on GitHub.
  • Create a feature branch: git checkout -b feature-name.
  • Commit your changes: git commit -m "Add new feature".
  • Push to the branch: git push origin feature-name.
  • Submit a pull request.

License

This project is licensed under the MIT License.

Acknowledgments

  • Thanks to the .NET community for inspiration and support.
  • Special thanks to all contributors who have helped improve this package.
Product Compatible and additional computed target framework versions.
.NET 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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  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.

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.2 120 1/2/2025
1.0.1 109 12/27/2024
1.0.0 99 12/27/2024