Api.ResultWrapper.AspNetCore 1.0.0

dotnet add package Api.ResultWrapper.AspNetCore --version 1.0.0
NuGet\Install-Package Api.ResultWrapper.AspNetCore -Version 1.0.0
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="Api.ResultWrapper.AspNetCore" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Api.ResultWrapper.AspNetCore --version 1.0.0
#r "nuget: Api.ResultWrapper.AspNetCore, 1.0.0"
#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 Api.ResultWrapper.AspNetCore as a Cake Addin
#addin nuget:?package=Api.ResultWrapper.AspNetCore&version=1.0.0

// Install Api.ResultWrapper.AspNetCore as a Cake Tool
#tool nuget:?package=Api.ResultWrapper.AspNetCore&version=1.0.0

A REST API global exception handler and response wrapper for ASP.NET Core APIs.

Api.ResultWrapper.AspNetCore

The Api.ResultWrapper.AspNetCore 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 Api.Helper.ContentWrapper.Core middleware into your ASP.NET Core app:

  1. Declare the following namespace within Startup.cs

    using Api.ResultWrapper.AspNetCore.Extensions;
    
  2. 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.

NOTE

Conditional middleware with UseWhen

The final case I want to look at is when you want most of your middleware to run for all requests but you have some conditional pieces - specific middleware that should only run for certain requests.

This is easily achieved with UseWhen which also uses a predicate to determine if the middleware should run:

app.UseWhen(context => context.Request.Path.StartsWithSegments("/api"), appBuilder =>
{
	appBuilder.UseAPIResponseWrapperMiddleware();
});

This code uses different error handling middleware when the request is for the API (determined using the URL). The predicate approach provides a lot of flexibility and you can conditionally apply middleware based on cookies, headers, current user and much more.

Don't Forget to register your modelstate error filter helper

 services.AddMvc(
              options =>
              {
                  options.Filters.Add(typeof(ModelStateFeatureFilter));  //this will allow you to 
                  //options.OutputFormatters.Add(new PascalCaseJsonProfileFormatter());
              })
         .AddJsonOptions(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver())
         .SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

Disable Automatic Model State Validation

You can remove the APIController attribute to disable the automatic model validation. But, then you will lose the other benefits of this attribute like disabling conventional routing and allowing model binding without adding [FromBody] parameter attributes.

The better approach to disable the default behavior by setting SuppressModelStateInvalidFilter option to true. You can set this option to true in the ConfigureServices method. Like,

   public void ConfigureServices(IServiceCollection services)
{
    services.Configure<ApiBehaviorOptions>(options =>
    {
	options.SuppressModelStateInvalidFilter = true;
    });
}

This will disable the automatic model state validation and now you can return the custom error from the controller action methods.

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.1 is compatible.  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
1.0.0 651 6/14/2019

A REST API global exception handler and response wrapper for ASP.NET Core APIs.