Giter Club home page Giter Club logo

vs1121 / asp.net-core-api-content-wrapper Goto Github PK

View Code? Open in Web Editor NEW

This project forked from pradeept95/asp.net-core-api-content-wrapper

0.0 0.0 0.0 2.31 MB

The Api.Helper.ContentWrapper.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.

C# 91.72% HTML 7.64% CSS 0.50% JavaScript 0.13%

asp.net-core-api-content-wrapper's Introduction

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.

asp.net-core-api-content-wrapper's People

Contributors

pradeept95 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.