CodexBloom - Programming Q&A Platform

Visual Studio 2022 - Debugging guide with ASP.NET Core Web API When Using Swagger

πŸ‘€ Views: 38 πŸ’¬ Answers: 1 πŸ“… Created: 2025-06-09
aspnet-core swagger visual-studio debugging csharp

Hey everyone, I'm running into an issue that's driving me crazy. Quick question that's been bugging me - I'm experiencing a frustrating scenario while debugging my ASP.NET Core Web API in Visual Studio 2022..... I have Swagger set up for API documentation, but when I start debugging and try to access the Swagger UI at `http://localhost:5000/swagger`, I get a `404 Not Found` behavior. The API works perfectly when running without the debugger, and I can access the endpoints directly. Here's a snippet of my `Startup.cs`: ```csharp public void ConfigureServices(IServiceCollection services) { services.AddControllers(); services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" }); }); } public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseRouting(); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); app.UseSwagger(); app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1"); }); } ``` I've checked that the `UseSwagger` and `UseSwaggerUI` methods are called in the correct order, and they are located after the `app.UseRouting()` line but before `app.UseEndpoints()`. I've also tried cleaning and rebuilding the solution, and even restarted Visual Studio. Could this be a result of the launch settings? My `launchSettings.json` looks like this: ```json { "profiles": { "My API": { "commandName": "Project", "launchBrowser": true, "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } } } } ``` I've gone through the configuration multiple times but still can’t identify the question. Any suggestions on why the Swagger UI might not be accessible during debugging? Are there specific settings or configurations in Visual Studio 2022 that could affect this behavior? I'm working on a web app that needs to handle this. Is there a better approach? I'm working with Csharp in a Docker container on Debian. What's the correct way to implement this? The project is a CLI tool built with Csharp. I appreciate any insights!