KnowYourToolset.ApiComponents 1.0.2

dotnet add package KnowYourToolset.ApiComponents --version 1.0.2
                    
NuGet\Install-Package KnowYourToolset.ApiComponents -Version 1.0.2
                    
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="KnowYourToolset.ApiComponents" Version="1.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="KnowYourToolset.ApiComponents" Version="1.0.2" />
                    
Directory.Packages.props
<PackageReference Include="KnowYourToolset.ApiComponents" />
                    
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 KnowYourToolset.ApiComponents --version 1.0.2
                    
#r "nuget: KnowYourToolset.ApiComponents, 1.0.2"
                    
#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.
#addin nuget:?package=KnowYourToolset.ApiComponents&version=1.0.2
                    
Install KnowYourToolset.ApiComponents as a Cake Addin
#tool nuget:?package=KnowYourToolset.ApiComponents&version=1.0.2
                    
Install KnowYourToolset.ApiComponents as a Cake Tool

KnowYourToolset.ApiComponents

NuGet version

Getting Started

The features in this ApiComponents package are:

  • ProblemDetails middleware that can be used by API projects and easily return a customizable ProblemDetails object when errors (unhandled exceptions) occur

Using the ProblemDetails middleware

To use the middleware for exception handling, simply call the UseProblemDetailsHandler extension method in the Configure method in Startup.cs.

You can call UseProblemDetailsHandler with no parameters to use defaults, and it will return a base-level ProblemDetails object with a variety of its properties filled in whenever an unhandled exception occurs.

If you want to add some additional context to the ProblemDetails response or use something other than 500-InternalServerError for the HTTP status, just use the AddResponseDetails option and provide a delegate method with your own logic (the CustomizeResponse method in the example below).

To do that, your Configure method might end up looking something like this:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    app.UseProblemDetailsHandler(options => options.AddResponseDetails = CustomizeResponse);

    //... more code here ...
}

private HttpStatusCode CustomizeResponse(HttpContext httpContext, Exception exception, ProblemDetails problemDetails)
{
    if (exception is ApplicationException appEx)
    {
        problemDetails.Detail = appEx.Message;
        return HttpStatusCode.BadRequest;
    }
    return HttpStatusCode.InternalServerError; 
}

Sample: A sample API that uses this is the KnowYourToolset.BackEnd template in the KnowYourToolset.Templates package. You can create a project using this template if you want to experiment with it.

Contributing

This is meant to be very simple and minimalistic features that can support ASP.NET Core+ API projects.

Feel free to use this code as a starter for things you might want to do in the same direction for your projects, or if you feel that something belongs in here, use the following workflow:

  • Fork the repo
  • Create a branch
  • Make your changes (and test them!) in the branch you created
  • Commit your changes
  • Submit a Pull Request
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.  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. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos 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.2 607 8/12/2021
1.0.2-alpha.0.4 175 8/12/2021
1.0.2-alpha.0.3 173 8/12/2021
1.0.1 394 8/7/2021
1.0.1-alpha.0.1 232 8/7/2021
1.0.0 403 8/7/2021
0.0.0-alpha.0.3 277 8/7/2021
0.0.0-alpha.0.2 162 8/7/2021
0.0.0-alpha.0.1 186 8/6/2021