Class ApplicationBuilderExtensions
- Namespace
- Codebelt.Extensions.Asp.Versioning
- Assembly
- Codebelt.Extensions.Asp.Versioning.dll
Extension methods for the IApplicationBuilder interface.
public static class ApplicationBuilderExtensions
- Inheritance
-
ApplicationBuilderExtensions
Examples
Add UseRestfulApiVersioning to the request pipeline when you want status codes written directly to the response — such as the 406 Not Acceptable or 415 Unsupported Media Type produced by Asp.Versioning when a client requests an unsupported version — translated into typed HttpStatusCodeException values that your exception-handling middleware can render. By default the middleware throws a mapped HttpStatusCodeException for status codes that Cuemon.AspNetCore.Http understands, falling back to InternalServerErrorException for everything else. Supply a custom factory when you need to surface a domain-specific exception or attach extra context to the failure.
using Cuemon.AspNetCore.Http;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
namespace Codebelt.Extensions.Asp.Versioning;
public class RestfulApiVersioningPipelineSetup
{
public static IApplicationBuilder ConfigurePipeline(IApplicationBuilder builder)
{
return builder.UseRestfulApiVersioning(context =>
{
if (HttpStatusCodeException.TryParse(context.Response.StatusCode, out var mappedException))
{
return mappedException;
}
return new InternalServerErrorException("Unmapped status code emitted by upstream middleware");
});
}
}
Methods
UseRestfulApiVersioning(IApplicationBuilder, Func<HttpContext, HttpStatusCodeException>)
Adds a middleware to the pipeline that will intercept status codes written directly to the response and throw an appropriate HttpStatusCodeException that can then be translated and re-written in a consistent way.
public static IApplicationBuilder UseRestfulApiVersioning(this IApplicationBuilder builder, Func<HttpContext, HttpStatusCodeException> statusCodeExceptionFactory = null)
Parameters
builderIApplicationBuilderThe type that provides the mechanisms to configure an application’s request pipeline.
statusCodeExceptionFactoryFunc<HttpContext, HttpStatusCodeException>The function delegate that will resolve and throw a proper HttpStatusCodeException from status codes written directly to the response.
Returns
- IApplicationBuilder
A reference to this instance after the operation has completed.
Remarks
This method was introduced because of the design decisions made of the author of Asp.Versioning; for more information have a read at https://github.com/dotnet/aspnet-api-versioning/issues/886
Exceptions
- ArgumentNullException
buildercannot be null.