[英]How to load the swagger page by default whenever browse the Azure API URL/Endpoint
I have created an.Net Core Web API application and then enabled the swagger definition for this API application.我创建了一个 .Net Core Web API 应用程序,然后为这个 API 应用程序启用了 swagger 定义。
This is the Startup.cs file这是 Startup.cs 文件
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// Register the Swagger generator, defining one or more Swagger documents
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1",
new Microsoft.OpenApi.Models.OpenApiInfo { Title = "Demo Web API", Version = "v1" });
});
//services.AddControllers();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseMvc();
// Enable middleware to serve generated Swagger as a JSON endpoint.
app.UseSwagger();
// Enable middleware to serve swagger-ui(HTML, JS, CSS, etc.),
// specifying the Swagger JSON endpoint.
app.UseSwaggerUI(options =>
{
options.SwaggerEndpoint("/swagger/v1/swagger.json", "Demo Web API");
//options.RoutePrefix = string.Empty;
//options.DocumentTitle = "Demo Web API";
//options.DocExpansion(DocExpansion.List);
});
}
}
I have published this application into Azure API App.我已将此应用程序发布到 Azure API App。 After that I'm trying to browse the Azure API App default URL then it's not loading the swagger page as default one.
之后,我尝试浏览 Azure API App default URL 然后它不会将 swagger 页面加载为默认页面。 If I want to see the Swagger UI then I need manually append “ /swagger ” at the end of Azure API App URL.
如果我想查看 Swagger UI,那么我需要在 Azure API App URL 的末尾手动输入 append “ /swagger ”。
I need to add the API App as a target into the backend pool of Azure Application Gateway.我需要将 API App 作为目标添加到 Azure 应用程序网关的后端池中。 Whenever I use public IP or DNS name of the Azure Application Gateway then it should display the Swagger UI of the Azure API App.
每当我使用 Azure 应用程序网关的公共 IP 或 DNS 名称时,它应该显示 Swagger Azure API App 的 UI。
I have updated the Configure()
method in Startup.cs
file to load the swagger page by default based on the environment.我更新了
Startup.cs
文件中的Configure()
方法,根据环境默认加载swagger页面。
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
// Enable middleware to serve generated Swagger as a JSON endpoint.
app.UseSwagger();
if (env.IsDevelopment())
{
app.UseSwaggerUI();
app.UseDeveloperExceptionPage();
}
else
{
// Enable middleware to serve swagger-ui(HTML, JS, CSS, etc.),
// specifying the Swagger JSON endpoint.
app.UseSwaggerUI(options =>
{
options.SwaggerEndpoint("/swagger/v1/swagger.json", "Demo Web API");
options.RoutePrefix = string.Empty;
//options.DocumentTitle = "Demo Web API";
//options.DocExpansion(DocExpansion.List);
});
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseMvc();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.