Swagger OpenApi

  • Swagger – Specification for describing REST APIs
  • Microsoft.AspNetCore.OpenApi package comes by default in .net 9
  • You can see your schema specification in below url. Modify your domain accordingly
    • https://localhost:7290/openapi/v1.json
  • Add your services in program.cs
    • builder.Services.AddOpenApi();
  • OpenApi doesnt come with UI,
  • So to get the best of both the world, we need to use OpenApi and Swagger for UI
    • Use below url to see the swagger UI
    • https://localhost:7290/swagger/
  • But instead of Swagger you can go ahead with different package, i.e Scalar
				
					builder.Services.AddOpenApi();

if (app.Environment.IsDevelopment())
{
    app.MapOpenApi();
    //only call you need to do
    app.UseSwaggerUI(options => {
        options.SwaggerEndpoint("/openapi/v1.json", "Demo Api");
    });

    //app.MapScalarApiReference();
    
    app.MapScalarApiReference(options =>
    {
      options.WithTitle("Demo Api")
      .WithTheme(ScalarTheme.Mars);
    });
}
				
			

Leave a Comment