-
Notifications
You must be signed in to change notification settings - Fork 3
/
ApplicationBuilderExtensions.cs
41 lines (40 loc) · 2.19 KB
/
ApplicationBuilderExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
using System;
using System.Threading.Tasks;
using Cuemon.AspNetCore.Http;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
namespace Cuemon.Extensions.Asp.Versioning
{
/// <summary>
/// Extension methods for the <see cref="IApplicationBuilder"/> interface.
/// </summary>
public static class ApplicationBuilderExtensions
{
/// <summary>
/// Adds a middleware to the pipeline that will intercept status codes written directly to the response and throw an appropriate <see cref="HttpStatusCodeException"/> that can then be translated and re-written in a consistent way.
/// </summary>
/// <param name="builder">The type that provides the mechanisms to configure an application’s request pipeline.</param>
/// <param name="statusCodeExceptionFactory">The function delegate that will resolve and throw a proper <see cref="HttpStatusCodeException"/> from status codes written directly to the response.</param>
/// <returns>A reference to this instance after the operation has completed.</returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="builder"/> cannot be null.
/// </exception>
/// <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</remarks>
public static IApplicationBuilder UseRestfulApiVersioning(this IApplicationBuilder builder, Func<HttpContext, HttpStatusCodeException> statusCodeExceptionFactory = null)
{
Validator.ThrowIfNull(builder);
statusCodeExceptionFactory ??= context =>
{
if (HttpStatusCodeException.TryParse(context.Response.StatusCode, out var statusCodeEquivalentException))
{
return statusCodeEquivalentException;
}
return new InternalServerErrorException();
};
return builder.UseStatusCodePages(app =>
{
app.Use((HttpContext context, Func<Task> _) => throw statusCodeExceptionFactory(context));
});
}
}
}