Skip to content

This library provides a generic response type compatible with IActionResult in Microsoft.AspNetCore.Mvc

License

Notifications You must be signed in to change notification settings

ogulcanturan/Ogu.AspNetCore.Response

Repository files navigation

Header Ogu.AspNetCore.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" });
}

output

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

example 2:

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

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.AspNetCore.Response can be found here.

About

This library provides a generic response type compatible with IActionResult in Microsoft.AspNetCore.Mvc

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages