Search code examples
c#asp.net-coreswagger

No webpage was found for the web address: https://localhost:7000/swagger/index.html


I'm trying to set up Swagger for my .Net Core 6 project but I get the below issue:

No webpage was found for the web address: https://localhost:7000/swagger/index.html

Issue

Below is my Program.cs class:

using Microsoft.EntityFrameworkCore;
using Microsoft.OpenApi.Models;
using MyWebApp.API.Mappers;
using MyWebApp.Data.DBContext;
using MyWebApp.Data.Mappers;
using MyWebApp.Data.Repositories.Implementation;
using MyWebApp.Domain.Business.Implementation;
using MyWebApp.Domain.Business.Interface;
using MyWebApp.Domain.Repositories.Interface;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddDbContext<MyWebAppDBContext>(options => options.UseSqlServer(builder.Configuration.GetConnectionString("MyAppDbConnection")));
builder.Services.AddControllers();

// AutoMapper
builder.Services.AddAutoMapper(
               typeof(RequestResponseMapper).Assembly,
               typeof(DataMapper).Assembly
               );

// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();

builder.Services.AddScoped<ITaskService, TaskService>();
builder.Services.AddScoped<ITaskRepository, TaskRepository>();

// Swagger config
builder.Services.AddSwaggerGen(c =>
{
    c.SwaggerDoc("v1", new OpenApiInfo { Title = "MyWebApp", Version = "v1" });
});

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Error");
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    app.UseHsts();
    app.UseSwagger();
    app.UseSwaggerUI(c =>
    {
        c.SwaggerEndpoint("/swagger/v1/swagger.json", "MyWebApp v1");
        c.DisplayOperationId();
        c.DisplayRequestDuration();
    });
}

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

using (var scope = app.Services.CreateScope())
{
    var services = scope.ServiceProvider;

    var context = services.GetRequiredService<MyWebAppDBContext>();
    context.Database.Migrate();
}

app.Run();

and my launchsettings.json:

{
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:13145",
      "sslPort": 44344
    }
  },
  "profiles": {
    "MyWebApp": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "launchUrl": "swagger",
      "applicationUrl": "https://localhost:7000;http://localhost:5000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "launchUrl": "swagger",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

What am I doing wrong here? Any help would be great.


Solution

  • it was running in development env,and the related middleware( generate swagger page ) won't be called,so the page won't be generated,then you got 404 err

    if (!app.Environment.IsDevelopment())
    {
        app.UseExceptionHandler("/Error");
        // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
        app.UseHsts();
        app.UseSwagger();
        app.UseSwaggerUI(c =>
        {
            c.SwaggerEndpoint("/swagger/v1/swagger.json", "MyWebApp v1");
            c.DisplayOperationId();
            c.DisplayRequestDuration();
        });
    }
    

    it should be

    if (app.Environment.IsDevelopment())
    {
        app.UseSwagger();
        app.UseSwaggerUI(.....);
    }