Sane.Http.HttpResponseExceptions 2.0.0

dotnet add package Sane.Http.HttpResponseExceptions --version 2.0.0
                    
NuGet\Install-Package Sane.Http.HttpResponseExceptions -Version 2.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="Sane.Http.HttpResponseExceptions" Version="2.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Sane.Http.HttpResponseExceptions" Version="2.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Sane.Http.HttpResponseExceptions" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Sane.Http.HttpResponseExceptions --version 2.0.0
                    
#r "nuget: Sane.Http.HttpResponseExceptions, 2.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.
#:package Sane.Http.HttpResponseExceptions@2.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Sane.Http.HttpResponseExceptions&version=2.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Sane.Http.HttpResponseExceptions&version=2.0.0
                    
Install as a Cake Tool

Sane.Http.HttpResponseException

HttpResponseException and corresponding middleware and helpers.

Motivation

ASP.NET MVC used to have HttpResponseException which when thrown was caught by the framework and converted to the specified status code response. This was removed in ASP.NET Core because it was misused, namely people would throw it from their business logic layer. Some people also feel like it is a bad practice to treat 400 and 500 responses as exceptions (we disagree).

One thing you could do with HttpResponseException is create methods to return a specific status code on specific conditions. For example a common pattern would be something like

public async Task<Post> Get(Guid id)
{
    Post? post = await PostService.GetFeedPostAsync(id);
    ThrowNotFoundIfNull(post);

    return post;
}

The alternative without HttpResponseException looks like this

public async Task<ActionResult<Post>> Get(Guid id)
{
    Post? post = await PostService.GetFeedPostAsync(id);

    if (post is null)
    {
        return NotFound();
    }

    return post;
}

The problem with this approach is that the developer can't hide the ifs in a method. Sometimes the ifs might be more complex and will need specific method call for the condition. Using exceptions allows to return the status code directly from the method.

To use HttpResponseExceptions add the middleware

app.UseHttpResponseExceptions();

and use the ThrowHelper class or throw the exception yourself. You can also add a global static using for the ThrowHelper or you can add helper methods in your base controller class.

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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.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
2.0.0 3,283 1/24/2024
1.1.1 2,551 11/16/2022
1.1.0 729 8/28/2022
1.0.0 794 12/13/2021