I’m trying to get Umbraco to always generate the swagger schema when publishing the site for deployment. Has anyone else done this before? The idea is that this site is open and anyone can consume the schema to learn how to talk to the Umbraco instance.
I’ve managed to “hack” it together with an update to my .csproj
, I added the SwashBuckle.AspNetCode
NuGet package and added a target:
<Target Name="GenerateSwagger" BeforeTargets="Publish">
<!-- Generate swagger before publish to avoid file locking issues -->
<PropertyGroup>
<SwaggerOutputFile>wwwroot\swagger\delivery-swagger.json</SwaggerOutputFile>
</PropertyGroup>
<!-- Make sure the output directory exists -->
<MakeDir Directories="wwwroot\swagger" />
<!-- Run Swashbuckle CLI to generate swagger.json from built assembly -->
<Exec Command="dotnet tool restore" />
<Exec Command="dotnet swagger tofile --output "$(SwaggerOutputFile)" "$(TargetDir)$(ProjectName).dll" delivery" />
</Target>
I don’t like it very much though, the Exec Command
is annoying and feels unsustainable.
Is there a better way, without just enabling Swagger on production?