CodexBloom - Programming Q&A Platform

ASP.NET Core 7: Swagger Not Displaying API Endpoints Due to Incorrect Route Configuration

πŸ‘€ Views: 187 πŸ’¬ Answers: 1 πŸ“… Created: 2025-06-13
aspnet-core swagger dotnet-7 C#

Hey everyone, I'm running into an issue that's driving me crazy. I'm having an scenario where the Swagger UI in my ASP.NET Core 7 application is not displaying any of the API endpoints. I have verified that my controllers are decorated with the `[ApiController]` and `[Route]` attributes. My `Startup.cs` is configured to use Swagger, but when I navigate to `/swagger/index.html`, it shows no endpoints available. Here’s a snippet of my `Startup.cs`: ```csharp public void ConfigureServices(IServiceCollection services) { services.AddControllers(); services.AddEndpointsApiExplorer(); services.AddSwaggerGen(); } public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseRouting(); app.UseSwagger(); app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1"); c.RoutePrefix = string.Empty; }); app.UseAuthorization(); app.UseEndpoints(endpoints => { endpoints.MapControllers(); }); } ``` I also have a sample controller: ```csharp [ApiController] [Route("api/[controller]")] public class SampleController : ControllerBase { [HttpGet] public IActionResult Get() { return Ok(new { Message = "Hello World" }); } } ``` Despite this, Swagger still shows "No operations defined in spec!". I’ve tried adding `[ApiExplorerSettings(GroupName = "v1")]` to the controller, but that did not help. My project is running on .NET 7, and I’m using Swashbuckle.AspNetCore version 6.4.0. I suspect it might be a routing scenario, but I’m not sure how to diagnose it further. Any insights into what might be going wrong? Any pointers in the right direction?