I have created an ASP.NET Core 6.0 MVC (not API) project with one API controller only. It’s working when we go to the url directly in a browser, but Swagger is not working. Please let me know how can I do this.
Installed all the required packages.
Make sure you have NuGet installed(because I dont know what “all required packages” means)
Try this Program.cs sample implementation for swagger
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
});
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
and also put the annotations
[ApiController]
[Route("api/[controller]")]
to your controller class
Cosmin Drăguşanu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.