Last active
March 11, 2026 16:21
-
-
Save dj-nitehawk/dc0018ab4a446764ef3c87213bf96959 to your computer and use it in GitHub Desktop.
Export Swagger JSON files during compile time
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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