简体   繁体   English

每当浏览 Azure API URL/端点时,如何默认加载 swagger 页面

[英]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.

相关问题 默认 swagger 索引页面在 Azure 中默认不加载 - Default swagger index page not loading by default in Azure 获取/生成 swagger url 的 Azure APIm API - Get/generate swagger url of Azure APIm API 如何在 spring 云和 azure 应用程序中公开开放 API 端点 - How to expose open-API endpoint in spring cloud and azure application 有没有办法通过订阅限制 Azure API 管理端点? - Is There a Way to Restrict Azure API Management Endpoint By Subscription? 如何解决,无法加载默认凭据。 浏览至 https://cloud.google.com/docs/authentication/getting-started 了解更多信息 - How to solve , Could not load the default credentials. Browse to https://cloud.google.com/docs/authentication/getting-started for more information 如何在azure功能中浏览或下载/home的内容? - How can I browse or download the content of /home in azure functions? Azure Static Web App - Function App API - 如何加载 IOptions? - Azure Static Web App - Function App API - How to load IOptions? 如何从命令行界面获取 aws api 网关端点 url - How to get aws api gateway endpoint url from command line interface Azure URL 应用程序服务无法在 .net 中使用专用端点 - Azure URL app service not working in vnet with private endpoint Azure API 管理-可用性测试-如何获取APIM的HealthCheck url - Azure API Management - Availability Tests - How to get the HealthCheck url of APIM
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM