diff --git a/aspnetcore/fundamentals/openapi/aspnetcore-openapi.md b/aspnetcore/fundamentals/openapi/aspnetcore-openapi.md index 69a1295fc2d2..23626b6a9318 100644 --- a/aspnetcore/fundamentals/openapi/aspnetcore-openapi.md +++ b/aspnetcore/fundamentals/openapi/aspnetcore-openapi.md @@ -5,7 +5,7 @@ description: Learn how to generate and customize OpenAPI documents in an ASP.NET ms.author: safia monikerRange: '>= aspnetcore-6.0' ms.custom: mvc -ms.date: 12/11/2024 +ms.date: 01/23/2025 uid: fundamentals/openapi/aspnetcore-openapi --- # Generate OpenAPI documents @@ -48,7 +48,7 @@ The following code: * Adds OpenAPI services using the extension method on the app builder's service collection. * Maps an endpoint for viewing the OpenAPI document in JSON format with the extension method on the app. -[!code-csharp[](~/fundamentals/openapi/samples/9.x/WebMinOpenApi/Program.cs?name=snippet_first&highlight=3,7)] +[!code-csharp[](~/fundamentals/openapi/samples/9.x/WebMinOpenApi/Program.cs?name=snippet_first&highlight=3,9)] Launch the app and navigate to `https://localhost:/openapi/v1.json` to view the generated OpenAPI document. diff --git a/aspnetcore/fundamentals/openapi/samples/9.x/WebMinOpenApi/Program.cs b/aspnetcore/fundamentals/openapi/samples/9.x/WebMinOpenApi/Program.cs index 3de9cb2e1f80..cd215fece33c 100644 --- a/aspnetcore/fundamentals/openapi/samples/9.x/WebMinOpenApi/Program.cs +++ b/aspnetcore/fundamentals/openapi/samples/9.x/WebMinOpenApi/Program.cs @@ -3,7 +3,6 @@ //#define DOCUMENTtransformer1 //#define DOCUMENTtransformer2 #define DOCUMENTtransformerUse999 -//#define DEFAULT //#define FIRST //#define OPENAPIWITHSCALAR //#define MAPOPENAPIWITHCACHING @@ -80,7 +79,10 @@ internal record WeatherForecast(DateTime Date, int TemperatureC, string? Summary var app = builder.Build(); -app.MapOpenApi(); +if (app.Environment.IsDevelopment()) +{ + app.MapOpenApi(); +} app.MapGet("/", () => "Hello world!"); @@ -107,7 +109,10 @@ internal record WeatherForecast(DateTime Date, int TemperatureC, string? Summary var app = builder.Build(); -app.MapOpenApi(); +if (app.Environment.IsDevelopment()) +{ + app.MapOpenApi(); +} app.MapGet("/", () => "Hello world!"); @@ -161,7 +166,10 @@ public async Task TransformAsync(OpenApiDocument document, OpenApiDocumentTransf var app = builder.Build(); -app.MapOpenApi(); +if (app.Environment.IsDevelopment()) +{ + app.MapOpenApi(); +} app.MapGet("/", () => "Hello world!"); @@ -189,7 +197,10 @@ public async Task TransformAsync(OpenApiDocument document, OpenApiDocumentTransf var app = builder.Build(); -app.MapOpenApi(); +if (app.Environment.IsDevelopment()) +{ + app.MapOpenApi(); +} app.MapGet("/world", () => "Hello world!") .WithGroupName("internal"); @@ -253,7 +264,10 @@ public async Task TransformAsync(OpenApiDocument document, OpenApiDocumentTransf var app = builder.Build(); -app.MapOpenApi(); +if (app.Environment.IsDevelopment()) +{ + app.MapOpenApi(); +} app.MapGet("/", () => new Body { Amount = 1.1m }); @@ -279,9 +293,10 @@ public class Body { var app = builder.Build(); -app.MapOpenApi(); if (app.Environment.IsDevelopment()) { + app.MapOpenApi(); + app.UseSwaggerUI(options => { options.SwaggerEndpoint("/openapi/v1.json", "v1"); @@ -342,8 +357,11 @@ public class Body { app.UseOutputCache(); -app.MapOpenApi() - .CacheOutput(); +if (app.Environment.IsDevelopment()) +{ + app.MapOpenApi() + .CacheOutput(); +} app.MapGet("/", () => "Hello world!"); @@ -365,10 +383,9 @@ public class Body { var app = builder.Build(); -app.MapOpenApi(); - if (app.Environment.IsDevelopment()) { + app.MapOpenApi(); app.MapScalarApiReference(); } @@ -386,7 +403,10 @@ public class Body { var app = builder.Build(); -app.MapOpenApi(); +if (app.Environment.IsDevelopment()) +{ + app.MapOpenApi(); +} app.MapGet("/", () => "Hello world!"); @@ -419,7 +439,10 @@ public class Body { var app = builder.Build(); -app.MapOpenApi(); +if (app.Environment.IsDevelopment()) +{ + app.MapOpenApi(); +} app.MapGet("/", () => "Hello world!"); @@ -473,7 +496,10 @@ public Task TransformAsync(OpenApiSchema schema, OpenApiSchemaTransformerContext var app = builder.Build(); -app.MapOpenApi(); +if (app.Environment.IsDevelopment()) +{ + app.MapOpenApi(); +} app.MapGet("/", () => "Hello world!");