com.protostream.nuget.badrequest 1.2.0

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

// Install com.protostream.nuget.badrequest as a Cake Tool
#tool nuget:?package=com.protostream.nuget.badrequest&version=1.2.0                

BadRequestHandlingMiddleware NuGet Package

BadRequestHandlingMiddleware is a .NET middleware package designed for handling BadRequestException errors gracefully in ASP.NET Core applications. This middleware catches BadRequestExceptions, formats the response as JSON, and sends it with an appropriate HTTP status code.

Features

  • Custom BadRequestException for managing bad request errors with detailed error information.
  • Middleware that catches BadRequestException and formats the response for clients.
  • JSON output with camel-cased property names using native System.Text.Json libraries.

Installation

Install via NuGet Package Manager:

dotnet add package com.protostream.nuget.badrequest

Or in Visual Studio:

  1. Go to Project > Manage NuGet Packages...
  2. Search for com.protostream.nuget.badrequest.
  3. Install the package.

Usage

1. Register the Middleware

In your Startup.cs or Program.cs (for .NET 6+), add the middleware to the HTTP request pipeline:

using com.protostream.nuget.badrequest;

public class Startup
{
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        app.UseBadRequestExceptionHandling();

        // other middleware registrations
        app.UseRouting();
        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
        });
    }
}

2. Throw BadRequestException in Controllers or Services

Use the BadRequestException to trigger a handled bad request response:

using com.protostream.nuget.badrequest;
using Microsoft.AspNetCore.Mvc;

public class SampleController : ControllerBase
{
    [HttpGet("api/sample")]
    public IActionResult Get()
    {
        throw new BadRequestException("ExampleKey", "An example error occurred.");
    }
}

Example JSON Response

When BadRequestException is caught, the middleware returns a JSON response with a 400 status code:

{
  "exampleKey": ["An example error occurred."]
}

Classes

  • BadRequestException: A custom exception class for handling bad requests with detailed model state errors.
  • BadRequestHandlingMiddleware: Middleware that catches BadRequestException, sets the HTTP status to 400, and formats the error as JSON.
  • BadRequestHandlingMiddlewareExtension: Extension method for easily registering the middleware in the ASP.NET Core request pipeline.

License

This package is licensed under the MIT License.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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. 
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.2.0 44 11/27/2024
1.0.0 187 10/14/2024