DeveloperSD
DeveloperSD

Reputation: 316

Swagger page is not display as startup page in IIS for .net core 3.1 Web API

I have published web API application(.Net Core 3.1) on IIS under Default Web Site. I have follow the all necessary steps to host .net core application on IIS. Using postman tool I have checked that all API are working correctly.

As I want to show Swagger UI page as startup page after browsing the API from IIS. From IIS whenever I have clicked on [browse:XXX:XXX:XXX:XXX:443 (https) ] link it shows This XXX.XXX.XXX.XXX page can’t be found

browser URL : https://https/XXX.XXX.XXX.XXX/API

Note: If I have added "/" at end of above url then it automatically redirect to swagger's Index.html page.

My existing source code for swagger

public void ConfigureServices(IServiceCollection services)
{
    services.AddSwaggerGen(c =>
    {
        c.SwaggerDoc("v1.0.0", new Microsoft.OpenApi.Models.OpenApiInfo
        {
            Title = "TestAPI",
            Version = "v1.0.0",
            Description = "Test API"
        });

        // Set the comments path for the Swagger JSON and UI.
        var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
        var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
        c.IncludeXmlComments(xmlPath);
    });
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
   app.UseSwagger();
   app.UseSwaggerUI(c =>
   {
      c.SwaggerEndpoint("./swagger/v1.0.0/swagger.json", "TestAPI V1.0.0");
      c.RoutePrefix = "";
   });
}

In IIS I have added application by the name (API). I want to show swagger Index.html page as default page after accessing from IIS.

I don not understand that anything is missing at code level or need to do any setting on IIS to display default swagger page.

Additional Information:

Swagger Package: Swashbuckle.AspNeteCore(5.3.1)

OS: Windoes 10

IIS : 10.0.19041.1

Note: Same API I have published as NewWebSite(Not under Default Web Site) on IIS then it works as par expectation.

Upvotes: 7

Views: 15278

Answers (3)

kevin
kevin

Reputation: 1083

In my case, Swagger was working but it was just not showing as startup page. Adding "launchUrl": "swagger" under profiles in launchsettings.json worked for me.

{ 
  "iisSettings": {
    ...
  },
  "profiles": {
    "IIS Express": {
    "commandName": "IISExpress",
    "launchBrowser": true,
    "launchUrl": "swagger",
    "environmentVariables": {
      "ASPNETCORE_ENVIRONMENT": "Development"
    }
  },
  ...
}

Upvotes: 11

Muhammad Suleman
Muhammad Suleman

Reputation: 59

this settings works for me.

app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("v1/swagger.json", "My Api v1");
});

Upvotes: 0

Abraham Qian
Abraham Qian

Reputation: 7522

Basically, all you have done is enough to configure the Swagger UI extension.
After we set up the below code segments for the Swagger JSON file, the base address of the website will display the Swagger index page.

app.UseSwaggerUI(c =>
{
    c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
    c.RoutePrefix = string.Empty;
});

At the same time, the Swagger.json file can be generated properly when we configured the XML comments path.

var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
c.IncludeXmlComments(xmlPath);

Official tutorial.
https://learn.microsoft.com/en-us/aspnet/core/tutorials/getting-started-with-swashbuckle?view=aspnetcore-3.1&tabs=visual-studio
There might be something wrong with the IIS configuration. On my side, both the default web site and a new website work properly. I doubt if there are some URL rules in your default website. Please check if the URL rules exist under the URL Rewrite extension of the default web site.
Besides, please clear the local browser cache, or verify it in a private window of the browser, after we removed some URL rules.
Feel free to let me know if the problem still exists.

Upvotes: -2

Related Questions