繁体   English   中英

.NET 6 WebApi:如何打开 Swagger 索引。html 默认在 Z5DA5ACF461B4EFB27E76EC8610566 上

[英].NET 6 WebApi: how to open Swagger index.html by default on IIS

I can't understand how to teach to the web api and IIS to open the default page index.html of Swagger when navigate into. 我想将默认 WebApi 页面重定向到 Swagger。

WebApi 位于“默认 Web 站点”中。

我尝试了以下方法:

if (app.Environment.IsDevelopment())
{
    app.UseSwaggerUI();
}
else if (app.Environment.IsProduction())
{
    app.UseSwaggerUI(options =>
    {
        options.SwaggerEndpoint("/swagger/v1.0/swagger.json", "v1.0");
        options.RoutePrefix = "api/swagger";
    });
}

IIS 打开默认“https://localhost/WebApi”显示

{"Message":"值不能为 null。(参数'key')","StatusCode":500}

有什么建议么?

谢谢

您可以将launchUrl 添加到launch.json 文件中。 您可以为每个配置文件配置不同的 launchUrl。

"TestWebApplication": {
  "launchBrowser": true,
  "launchUrl": "swagger",
  "applicationUrl": "https://localhost:5001;http://localhost:5000",
  "environmentVariables": {
    "ASPNETCORE_ENVIRONMENT": "Development"
  }
}

尝试删除 if else 块。

在 Configure 方法中的 Startup.cs 上,我刚刚

app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "API NAME 1.0.0.0"));

然后在 ConfigureService 方法中:

    services.AddSwaggerGen(c =>
    {
        c.CustomSchemaIds(x => x.FullName);
        c.SwaggerDoc("v1",
            new OpenApiInfo { Title = "MyApi", Version = "v1" });
        var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
        var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
        c.IncludeXmlComments(xmlPath);
    });

它适用于生产和开发模式,使用以下网址

发展:

  • https://localhost:44388/swagger/index.html

生产:

  • https://mywebsite/swagger/index.html(在您的情况下可能是 https://localhost/swagger/index.html)

Go to debug=>{ProjectName} Debug Properties 在“Url”字段写入你想要的第一页

1)

在此处输入图像描述

2) 在此处输入图像描述

3) 在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM