-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathSwaggerUiIndexMiddleware.cs
37 lines (34 loc) · 1.49 KB
/
SwaggerUiIndexMiddleware.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
using System.Reflection;
using Microsoft.AspNetCore.Http;
namespace NSwag.AspNetCore.Middlewares
{
internal sealed class SwaggerUiIndexMiddleware
{
private readonly RequestDelegate _nextDelegate;
private readonly string _indexPath;
private readonly SwaggerUiSettingsBase _settings;
private readonly string _resourcePath;
public SwaggerUiIndexMiddleware(RequestDelegate nextDelegate, string indexPath, SwaggerUiSettingsBase settings, string resourcePath)
{
_nextDelegate = nextDelegate;
_indexPath = indexPath;
_settings = settings;
_resourcePath = resourcePath;
}
public async Task Invoke(HttpContext context)
{
if (context.Request.Path.HasValue && string.Equals(context.Request.Path.Value.Trim('/'), _indexPath.Trim('/'), StringComparison.OrdinalIgnoreCase))
{
var stream = typeof(SwaggerUiIndexMiddleware).GetTypeInfo().Assembly.GetManifestResourceStream(_resourcePath);
using var reader = new StreamReader(stream);
context.Response.Headers["Content-Type"] = "text/html; charset=utf-8";
context.Response.StatusCode = 200;
await context.Response.WriteAsync(await _settings.TransformHtmlAsync(await reader.ReadToEndAsync(), context.Request, context.RequestAborted));
}
else
{
await _nextDelegate(context);
}
}
}
}