Skip to content

Latest commit

 

History

History
82 lines (67 loc) · 2.48 KB

README.md

File metadata and controls

82 lines (67 loc) · 2.48 KB

Header Ogu.Response

.NET Core Desktop NuGet Nuget

Introduction

Provides a generic response type (IResponse) compatible with IActionResult in Microsoft.AspNetCore.Mvc. This library is designed to simplify API responses by offering a structured model that can contain data, errors, validation errors, and additional extensions.

Features

  • Generic Response: A flexible and generic response model that can hold various types of data, including single objects, collections, or custom data structures.
  • Error Handling: Easily handle errors and exceptions in API responses, providing consistent error formats.
  • Validation Errors: Supports validation errors, making it easy to report validation issues in the response.
  • Extensions: Includes support for adding custom extensions to the response for specific use cases.

Installation

You can install the library via NuGet Package Manager:

dotnet add package Ogu.AspNetCore.Response.Json

Usage

example 1:

public IActionResult GetExample1()
{
    return HttpStatusCode.OK.ToSuccessResponse(new string[]{ "Freezing", "Bracing", "Chilly" }).ToAction();
}

output

{
  "data": [
    "Freezing",
    "Bracing",
    "Chilly"
  ],
  "success": true,
  "status": 200
}

example 2:

public IActionResult GetExample2()
{
    return HttpStatusCode.OK.ToFailureJsonResponse(ErrorKind.EXAMPLE_ERROR_OCCURRED).ToAction();
}

output

{
  "success": false,
  "status": 200,
  "result": {
    "title": "Bad Request",
    "status": 400,
    "detail": "Custom failure occurred.",
    "extensions": {
      "errors": [
        {
          "title": "EXAMPLE_ERROR_OCCURRED",
          "description": "Don't worry, everything's gonna be alright",
          "code": "0",
          "type": 0
        }
      ]
    }
  }
}

Sample Application

A sample application demonstrating the usage of Ogu.Response can be found here.