簡體   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