In my .NET 6 microservice I am generating the swagger.json file with Swashbuckle CLI package and the post build commands. Now I would like to use this json instead of the one generated at runtime by the Swashbuckle package so that the response is faster than the current one. Is there a way to achieve this?
You can include the generated file in the build output and use the "UseStaticFiles()" method to serve it.
For example include the generated file in the output under a new directory called "swaggerfiles", then in the configuration you can have (for example):
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swaggerfiles/swagger.json", "My microservice v1"));
app.UseStaticFiles(new StaticFileOptions()
{
FileProvider = new PhysicalFileProvider(Path.Combine(env.ContentRootPath, "swaggerfiles")),
RequestPath = "/swaggerfiles"
});