Skip to content

Instantly share code, notes, and snippets.

@dj-nitehawk
Last active March 11, 2026 16:21
Show Gist options
  • Select an option

  • Save dj-nitehawk/dc0018ab4a446764ef3c87213bf96959 to your computer and use it in GitHub Desktop.

Select an option

Save dj-nitehawk/dc0018ab4a446764ef3c87213bf96959 to your computer and use it in GitHub Desktop.
Export Swagger JSON files during compile time
<Project Sdk="Microsoft.NET.Sdk.Web">
...
<PropertyGroup>
<ExportSwaggerDocs>true</ExportSwaggerDocs>
<SwaggerExportPath>wwwroot/openapi</SwaggerExportPath>
</PropertyGroup>
<!-- this part is only needed for non-aot apps -->
<Target Name="SwaggerExport" AfterTargets="Build" Condition="'$(Configuration)'=='Release'">
<Exec WorkingDirectory="$(ProjectDir)" Command="dotnet '$(TargetPath)' --export-swagger-docs true"/>
</Target>
<ItemGroup>
<PackageReference Include="FastEndpoints" Version="8.0.1"/>
<PackageReference Include="FastEndpoints.Swagger" Version="8.0.1"/>
</ItemGroup>
</Project>
var bld = WebApplication.CreateBuilder(args);
bld.Services
.AddFastEndpoints()
.SwaggerDocument(o => o.DocumentSettings = d => d.DocumentName = "v1"); //must match below
var app = bld.Build();
app.UseFastEndpoints();
await app.ExportSwaggerDocsAndExitAsync("v1"); //must match doc names above
app.UseSwaggerGen();
app.Run();
// run 'dotnet build -c Release' or do a "Release" build in the IDE for the swagger json docs to be exported.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment