skip to Main Content

I’m using Visual Studio 2022, I have a new ASP.NET Core 8.0 Web API project. I removed all traces of the example weather forecast that was loaded when I created the project, but now when I debug my application, I get the following error:

This localhost page can’t be found.
No web page was found for the web address: https://localhost:7088/swagger

I also manually adjusted the URL but still get the error

No web page was found for the web address: https://localhost:7088/api/present

From what I can see, this setup is caused by incorrect settings in either the controller class, launchsettings.json or program.cs.

My controller code looks like this:

Controller class
[ApiController]
Route("api/[controller]")]
public class PresentController(IPresentsService _presentsService) : ControllerBase
{
    [HttpGet]
    [Produces(MediaTypeNames.Application.Json)]
    [ProducesResponseType(typeof(IEnumerable<PresentDto>), StatusCodes.Status200OK)]
    public async Task<IEnumerable<PresentDto>> GetAllPresentsAsync()
    {
        return await _presentsService.GetAllPresentsAsync();
    }
}

My launchSettings.json looks like this:

"https": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "launchUrl": "swagger",
      "applicationUrl": "https://localhost:7088;http://localhost:5030",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
}

My Program.cs looks like this:

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddControllers();

// Add services to the container.
builder.Services.AddScoped<IPresentsRepository, PresentsRepository>();
builder.Services.AddScoped<IPresentsService, PresentsService>();

builder.Services.AddDbContext<PresentsDbContext>(options =>
{
    options.UseNpgsql(builder.Configuration.GetConnectionString("PresentsDb"));
});

builder.Services.AddEndpointsApiExplorer();

builder.Services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());

var app = builder.Build();

// Configure the HTTP request pipeline.
app.UseHttpsRedirection();

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

app.Run();

Can somebody please explain what I am doing wrong? I have tried a few variations with the likes of Swashbuckle too, SwaggerUI etc but I have not had any luck.

2

Answers


  1. Chosen as BEST ANSWER

    Brando's changes helped massively but I did have this error:

    An unhandled exception occurred while processing the request.
    InvalidOperationException: Unable to resolve service for type 'Swashbuckle.AspNetCore.Swagger.ISwaggerProvider' while attempting to Invoke middleware 'Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware'.
    

    To resolve this I also added the builder.Services.AddSwaggerGen() line in the Program.cs code, as shown below.

    builder.Services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());
    
    builder.Services.AddSwaggerGen();
    
    var app = builder.Build();
    

    My application is now loading the swagger page correctly.


  2. This localhost page can’t be found.
    No web page was found for the web address: https://localhost:7088/swagger

    The reason why you face the error is you set a RoutePrefix to the swagger path, but you still use the old one, you should remove the c.RoutePrefix = "";, then the path(https://localhost:7088/swagger) is right.

    No web page was found for the web address: https://localhost:7088/api/present

    Besides, inside the your program.cs, you removed the app.UseSwagger() and app.MapControllers(); , this will cause your second error, there is no route for the api controller and the swagger middelware will not generate the swagger json .

    The right codes should as below:

    var app = builder.Build();
    
    // Configure the HTTP request pipeline.
    app.UseHttpsRedirection();
    app.UseSwagger();
    app.UseSwaggerUI(c =>
    {
        c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
    
    });
    app.MapControllers();
    app.Run();
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search