To generate OpenApi.json
on build from an ASP.NET 8 Minimal API, follow these steps:
-
In Visual Studio for Windows 17.7.0 or later, create a new
ASP.NET Core API
project -
Follow these instructions to install
Swashbuckle.AspNetCore.Cli
as a local dotnet tool in your project -
Add these NuGet packages (or later versions) to the project:
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.0-preview.4.23260.4" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
</ItemGroup>
- Add this target to the
.csproj
file:
<!-- Generate openapi.json on build; see
https://github.com/domaindrivendev/Swashbuckle.AspNetCore#using-the-tool-with-the-net-core-30-sdk-or-later -->
<Target Name="OpenAPI" AfterTargets="Build" Condition="$(Configuration)=='Debug'">
<Exec Command="dotnet swagger tofile --output openapi.json $(TargetPath) v1" WorkingDirectory="$(TargetDir)" />
</Target>
-
In
Program.cs
, add the OpenApi services:builder.Services.AddEndpointsApiExplorer() .AddSwaggerGen();
and append
.WithOpenApi()
to theMapGroup()
statement:var todosApi = app.MapGroup("/todos").WithOpenApi();
(optional) if you also want to generate the Swagger UI under the
/swagger
url, add:if (app.Environment.IsDevelopment()) _ = app.UseSwagger().UseSwaggerUI();
-
Build the project; the build output will contain a line like:
Swagger JSON/YAML successfully written to ...\bin\Debug\net8.0\openapi.json
Does that snippet actually work for you on .NET 8?
I'm running into this issue: domaindrivendev/Swashbuckle.AspNetCore#1580
The JSON file is generated, but the minimal API endpoints (
paths
JSON property) are missing from it.