VMD.RESTApiResponseWrapper.Core 1.0.2

Suggested Alternatives

AutoWrapper.Core 1.0.0

Additional Details

Usage: https://github.com/proudmonkey/AutoWrapper

There is a newer version of this package available.
See the version list below for details.
dotnet add package VMD.RESTApiResponseWrapper.Core --version 1.0.2
NuGet\Install-Package VMD.RESTApiResponseWrapper.Core -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="VMD.RESTApiResponseWrapper.Core" Version="1.0.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add VMD.RESTApiResponseWrapper.Core --version 1.0.2
#r "nuget: VMD.RESTApiResponseWrapper.Core, 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.
// Install VMD.RESTApiResponseWrapper.Core as a Cake Addin
#addin nuget:?package=VMD.RESTApiResponseWrapper.Core&version=1.0.2

// Install VMD.RESTApiResponseWrapper.Core as a Cake Tool
#tool nuget:?package=VMD.RESTApiResponseWrapper.Core&version=1.0.2

VMD.RESTApiResponseWrapper.Core

The VMD.RESTApiResponseWrapper.Core is a global exception handler and response wrapper for ASP.NET Core APIs. It uses a middleware to capture exceptions and to capture HTTP response to build a consistent response object for both successful and error requests.

Prerequisite

Install Newtonsog.Json package

Installing

Below are the steps to use the VMD.RESTApiResponseWrapper.Core middleware into your ASP.NET Core app:

  1. Declare the following namespace within Startup.cs

using VMD.RESTApiResponseWrapper.Core.Extensions;

  1. Register the middleware below within the Configure() method of Startup.cs

app.UseAPIResponseWrapperMiddleware();

Note: Make sure to register it "before" the MVC middleware

  1. Done.

Sample Output

The following are examples of response output:

Here's the format for successful request with data:

{
    
	"Version": "1.0.0.0",
    
	"StatusCode": 200,
    
	"Message": "Request successful.",
    
	"Result": [
		"value1",
        
		"value2"
	]

}
  

Here's the format for successful request without data:

{
    
	"Version": "1.0.0.0",
    
	"StatusCode": 201,
    
	"Message": "Student with ID 6 has been created."

}

Here's the format for error request with validation errors:

{
    
	"Version": "1.0.0.0",
    
	"StatusCode": 400,
    
	"Message": "Request responded with exceptions.",
    
	"ResponseException": {
        
		"IsError": true,
        
		"ExceptionMessage": "Validation Field Error.",
        
		"Details": null,
        
		"ReferenceErrorCode": null,
        
		"ReferenceDocumentLink": null,
        
		"ValidationErrors": [
            
			{
                
				"Field": "LastName",
                
				"Message": "'Last Name' should not be empty."
            
			},
            
			{
                
				"Field": "FirstName",
                
				"Message": "'First Name' should not be empty."
            
			}
        ]
    
	}

}

Here's the format for error request

{
    
	"Version": "1.0.0.0",
    
	"StatusCode": 404,
    
	"Message": "Unable to process the request.",
    
	"ResponseException": {
        
		"IsError": true,
        
		"ExceptionMessage": "The specified URI does not exist. Please verify and try again.",

	        "Details": null,
        
		"ReferenceErrorCode": null,
        
		"ReferenceDocumentLink": null,
        
		"ValidationErrors": null
    
	}

} 

Using Custom Exception

This library isn't just a middleware, it also provides some objects that you can use for defining your own exception. For example, if you want to throw your own exception message, you could simply do:

throw new ApiException("Your Message",401, ModelStateExtension.AllErrors(ModelState));

The ApiException has the following parameters that you can set:

ApiException(string message,
             int statusCode = 500,
             IEnumerable<ValidationError> errors = null, 
             string errorCode = "", 
             string refLink = "")

Defining Your Own Response Object

Aside from throwing your own custom exception, You could also return your own custom defined Response json by using the ApiResponse object in your API controller. For example:

return new APIResponse(201,"Created");

The APIResponse has the following parameters:

APIResponse(int statusCode, 
	    string message = "", 
	    object result = null, 
            ApiError apiError = null, 
            string apiVersion = "1.0.0.0")

Source Code

The source code for this can be found at: (https://github.com/proudmonkey/RESTApiResponseWrapper.Core)

Author

  • Vincent Maverick Durano - Blog

License

This project is licensed under the MIT License - see the LICENSE.md file for details

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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. 
.NET Core netcoreapp2.0 is compatible.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 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
2.0.0 18,078 9/20/2019
2.0.0-rc 401 9/13/2019
1.0.4 28,363 10/16/2018
1.0.3 12,562 6/6/2018
1.0.2 1,030 4/20/2018
1.0.1 1,078 4/16/2018
1.0.0 1,035 4/16/2018

-handle successful request and build a response object from it for consistency
-added some optional parameters when throwing your own exception
-added Message field at the top root of the response object
-added readme.txt
-minor code enhancements