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
<PackageReference Include="com.protostream.nuget.badrequest" Version="1.2.0" />
paket add com.protostream.nuget.badrequest --version 1.2.0
#r "nuget: com.protostream.nuget.badrequest, 1.2.0"
// 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 BadRequestException
s, 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:
- Go to Project > Manage NuGet Packages...
- Search for
com.protostream.nuget.badrequest
. - 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 | Versions 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. |
-
net8.0
- Microsoft.AspNetCore.Mvc.Core (>= 2.1.38)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.