如何在浏览Azure API URL/Endpoint时默认加载招摇撞骗页面



我已经创建了一个.Net Core Web API应用程序,然后为这个API应用程序启用了swagger定义。

这是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);
});
}
}

我已将此应用程序发布到Azure API应用程序中。在那之后,我尝试浏览Azure API应用程序的默认URL,然后它不会将招摇撞骗页面加载为默认页面。如果我想查看Swagger UI,那么我需要在Azure API应用程序URL的末尾手动附加">/Swagger"。

我需要将API应用程序作为目标添加到Azure应用程序网关的后端池中。每当我使用Azure应用程序网关的公共IP或DNS名称时,它应该显示Azure API应用程序的Swagger UI。

我已经更新了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();
}

最新更新