Created
September 8, 2016 11:25
-
-
Save SebastianStehle/42a164793b9008f808038bdf0a0fc35d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//----------------------------------------------------------------------- | |
// <copyright file="SwaggerMiddleware.cs" company="NSwag"> | |
// Copyright (c) Rico Suter. All rights reserved. | |
// </copyright> | |
// <license>https://github.com/NSwag/NSwag/blob/master/LICENSE.md</license> | |
// <author>Rico Suter, mail@rsuter.com</author> | |
//----------------------------------------------------------------------- | |
using System; | |
using System.Collections.Generic; | |
using System.Threading.Tasks; | |
using Microsoft.AspNetCore.Http; | |
using NSwag.CodeGeneration.SwaggerGenerators; | |
using NSwag.CodeGeneration.SwaggerGenerators.WebApi; | |
namespace NSwag.AspNetCore | |
{ | |
public abstract class SwaggerMiddlewareBase | |
{ | |
private readonly RequestDelegate _nextDelegate; | |
protected SwaggerMiddleware(RequestDelegate nextDelegate) | |
{ | |
_nextDelegate = nextDelegate; | |
} | |
public async Task Invoke(HttpContext context) | |
{ | |
if (context.Request.Path.HasValue && string.Equals(context.Request.Path.Value.Trim('/'), _path.Trim('/'), StringComparison.OrdinalIgnoreCase)) | |
{ | |
context.Response.StatusCode = 200; | |
await context.Response.WriteAsync(GenerateSwagger(context)); | |
} | |
else | |
{ | |
await _nextDelegate(context); | |
} | |
} | |
protected abstract GenerateSwagger(HttpContext context); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment