我為 NopCommerce 4.5 開發了 API 插件。是否可以為此 API 添加 Swagger 檔案?怎樣才能做到?
uj5u.com熱心網友回復:
在插件中,你需要添加一個新的 NopStatup.cs 檔案并配置 Swagger 中間件。
例子:
public partial class PluginNopStartup : INopStartup
{
/// <summary>
/// Add and configure any of the middleware
/// </summary>
/// <param name="services">Collection of service descriptors</param>
/// <param name="configuration">Configuration of the application</param>
public void ConfigureServices(IServiceCollection services, IConfiguration configuration)
{
services.AddSwaggerGen(options =>
{
options.SwaggerDoc("v1", new OpenApiInfo
{
Version = "v1",
Title = "ToDo API",
Description = "An ASP.NET Core Web API for managing ToDo items",
TermsOfService = new Uri("https://example.com/terms"),
Contact = new OpenApiContact
{
Name = "Example Contact",
Url = new Uri("https://example.com/contact")
},
License = new OpenApiLicense
{
Name = "Example License",
Url = new Uri("https://example.com/license")
}
});
});
}
/// <summary>
/// Configure the using of added middleware
/// </summary>
/// <param name="app">Builder for configuring an application's request pipeline</param>
public void Configure(IApplicationBuilder app)
{
app.UseSwagger();
app.UseSwaggerUI();
}
/// <summary>
/// Gets order of this startup configuration implementation
/// </summary>
public int Order => 1;
}
參考資料:如何配置 Swagger 到 ASP.NET Core .
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/533363.html
