简体   繁体   中英

.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'd like to redirect the default WebApi page to Swagger.

The WebApi is into "Default Web Site".

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

{"Message":"Value cannot be null. (Parameter 'key')","StatusCode":500}

Any suggestions?

Thanks

You can add the launchUrl to the launch.json file. You can configure a different launchUrl for every profile.

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

try to remove the if else block.

On my Startup.cs inside Configure method I have just

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

then inside ConfigureService method:

    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

production:

  • https://mywebsite/swagger/index.html (in your case could be https://localhost/swagger/index.html)

Go to debug=>{ProjectName} Debug Properties write the first page you want in the "Url" field

1)

在此处输入图像描述

2) 在此处输入图像描述

3) 在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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