简体   繁体   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. 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. I'd like to redirect the default WebApi page to Swagger.我想将默认 WebApi 页面重定向到 Swagger。

The WebApi is into "Default Web Site". WebApi 位于“默认 Web 站点”中。

I tried the following:我尝试了以下方法:

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 open the default "https://localhost/WebApi" showing IIS 打开默认“https://localhost/WebApi”显示

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

Any suggestions?有什么建议么?

Thanks谢谢

You can add the launchUrl to the launch.json file.您可以将launchUrl 添加到launch.json 文件中。 You can configure a different launchUrl for every profile.您可以为每个配置文件配置不同的 launchUrl。

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

try to remove the if else block.尝试删除 if else 块。

On my Startup.cs inside Configure method I have just在 Configure 方法中的 Startup.cs 上,我刚刚

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

then inside ConfigureService method:然后在 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);
    });

it works on production and on development mode to with the follow url's它适用于生产和开发模式,使用以下网址

development:发展:

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

production:生产:

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

Go to debug=>{ProjectName} Debug Properties write the first page you want in the "Url" field Go to debug=>{ProjectName} Debug Properties 在“Url”字段写入你想要的第一页

1) 1)

在此处输入图像描述

2) 2) 在此处输入图像描述

3) 3) 在此处输入图像描述

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

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