mirror of
https://github.com/nsnail/IGeekFan.AspNetCore.Knife4jUI.git
synced 2025-04-13 19:52:50 +08:00
support config options file
This commit is contained in:
parent
233268c5e3
commit
03ee5dadb0
@ -2,29 +2,36 @@
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
#if NETSTANDARD2_0
|
||||
using IWebHostEnvironment = Microsoft.AspNetCore.Hosting.IHostingEnvironment;
|
||||
#endif
|
||||
|
||||
namespace IGeekFan.AspNetCore.Knife4jUI
|
||||
{
|
||||
public static class Knife4UIBuilderExtensions
|
||||
{
|
||||
public static IApplicationBuilder UseKnife4UI(this IApplicationBuilder app, Knife4UIOptions options)
|
||||
{
|
||||
return app.UseMiddleware<Knife4jUIMiddleware>(options);
|
||||
}
|
||||
public static IApplicationBuilder UseKnife4UI(
|
||||
this IApplicationBuilder app,
|
||||
Action<Knife4UIOptions> setupAction = null)
|
||||
{
|
||||
var options = new Knife4UIOptions();
|
||||
if (setupAction != null)
|
||||
using (var scope = app.ApplicationServices.CreateScope())
|
||||
{
|
||||
setupAction(options);
|
||||
options = scope.ServiceProvider.GetRequiredService<IOptionsSnapshot<Knife4UIOptions>>().Value;
|
||||
setupAction?.Invoke(options);
|
||||
}
|
||||
else
|
||||
{
|
||||
options = app.ApplicationServices.GetRequiredService<IOptions<Knife4UIOptions>>().Value;
|
||||
}
|
||||
|
||||
app.UseMiddleware<Knife4jUIMiddleware>(options);
|
||||
|
||||
return app;
|
||||
if (options.ConfigObject.Urls == null)
|
||||
{
|
||||
var hostingEnv = app.ApplicationServices.GetRequiredService<IWebHostEnvironment>();
|
||||
options.ConfigObject.Urls = new[] { new UrlDescriptor { Name = $"{hostingEnv.ApplicationName} v1", Url = "v1/swagger.json" } };
|
||||
}
|
||||
return app.UseKnife4UI(options);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user