部署到IIS后,Swagger UI页面不显示

发布于 2025-01-11 11:27:08 字数 378 浏览 0 评论 0原文

将 ASP.NET Core Web API 部署到 IIS 后,不显示 Swagger UI 页面。

配置部分:

app.UseSwaggerUI(c =>
        {
            if (env.IsDevelopment() || env.IsProduction())

            {
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "My Test1 Api v1");
            }
        });

配置服务:

services.AddSwaggerGen( );

After deploying an ASP.NET Core Web API to IIS, Swagger UI page is not displayed.

Configure section:

app.UseSwaggerUI(c =>
        {
            if (env.IsDevelopment() || env.IsProduction())

            {
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "My Test1 Api v1");
            }
        });

Configure service:

services.AddSwaggerGen( );

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

耀眼的星火 2025-01-18 11:27:08

添加c.RoutePrefix = string.Empty;,将会改变起始页。

默认情况下,URL 应为 https://ip:port/swagger/index.html。当你部署这个应用程序后,你会得到404错误,不用担心,你可以在url中附加swagger,它会显示index.html。

输入图片此处描述

public void ConfigureServices(IServiceCollection services)
{
    services.AddControllers();
    ...
    services.AddSwaggerGen();
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    ...
    app.UseSwagger();
    app.UseSwaggerUI(c =>
    {
        if (env.IsDevelopment() || env.IsProduction())

        {
            c.SwaggerEndpoint("/swagger/v1/swagger.json", "My Test1 Api v1");
            // Add this line, it will work for you
            c.RoutePrefix = string.Empty;
        }
    });

    app.UseHttpsRedirection();

    app.UseRouting();

    app.UseAuthorization();

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllers();
    });
}

Add c.RoutePrefix = string.Empty;, will change the start page.

By default the url should be https://ip:port/swagger/index.html. And after you delpoyed this app, you will get 404 error, don't worry, you can append swagger in the url, it will show the index.html.

enter image description here

public void ConfigureServices(IServiceCollection services)
{
    services.AddControllers();
    ...
    services.AddSwaggerGen();
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    ...
    app.UseSwagger();
    app.UseSwaggerUI(c =>
    {
        if (env.IsDevelopment() || env.IsProduction())

        {
            c.SwaggerEndpoint("/swagger/v1/swagger.json", "My Test1 Api v1");
            // Add this line, it will work for you
            c.RoutePrefix = string.Empty;
        }
    });

    app.UseHttpsRedirection();

    app.UseRouting();

    app.UseAuthorization();

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllers();
    });
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文