Yes — typical answer: "I configured Swagger/OpenAPI for our ASP.NET Core APIs, including XML comments, JWT authentication in Swagger UI and versioned documents." Current state of the tooling - .NET 9+ includes built-in OpenAPI document generation with Microsoft.AspNetCore.OpenApi (AddOpenApi and MapOpenApi). The templates no longer include Swashbuckle, so you add a UI such as Scalar or Swagger UI separately. - Swashbuckle.AspNetCore and NSwag are still widely used, especially in existing projects. Built-in approach (.NET 9 and later) builder.Services.AddOpenApi(); var app = builder.Build(); if (app.Environment.IsDevelopment()) { app.MapOpenApi(); // serves /openapi/v1.json app.MapScalarApiReference(); // Scalar UI (Scalar.AspNetCore package) } Swashbuckle approach builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new OpenApiInfo { Title = "Orders API", Version = "v1" }); c.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, "Orders.Api.xml")); c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme { Type = SecuritySchemeType.Http, Scheme = "bearer", BearerFormat = "JWT", Description = "Paste a JWT access token" }); c.AddSecurityRequirement(new OpenApiSecurityRequirement { { new OpenApiSecurityScheme { Reference = new OpenApiReference { Type = ReferenceType.SecurityScheme, Id = "Bearer" } }, Array.Empty<string>() } }); }); app.UseSwagger(); app.UseSwaggerUI(); Enable XML documentation in the project file: <GenerateDocumentationFile>true</GenerateDocumentationFile>. Making the documentation accurate - Describe responses: [ProducesResponseType(typeof(OrderDto), 200)], [ProducesResponseType(404)], or TypedResults and Produces<T>() in minimal APIs. - XML comments or [EndpointSummary] / [EndpointDescription] for summaries and descriptions. - API versioning (Asp.Versioning) with one document per version. - Examples for request bodies, and enums serialized as strings if that is what clients see. Security and environments - Usually expose Swagger UI only in non-production environments, or protect it with authentication, because it reveals the full API surface. - The OpenAPI document can still be published to an API portal (Azure API Management) for consumers. Other uses of the OpenAPI document - Generating typed clients for the frontend (NSwag, Kiota, openapi-typescript), contract testing, and importing into API gateways or Postman.
Ravindra Wadile · · 0 likes