Skip to content

Instantly share code, notes, and snippets.

@SebastianStehle
Created September 8, 2016 11:25
Show Gist options
  • Save SebastianStehle/42a164793b9008f808038bdf0a0fc35d to your computer and use it in GitHub Desktop.
Save SebastianStehle/42a164793b9008f808038bdf0a0fc35d to your computer and use it in GitHub Desktop.
//-----------------------------------------------------------------------
// <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