Emanuele
Emanuele

Reputation: 93

Use swagger.json file generated in post build actions in Swashbuckle instead of the file generated at runtime

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?

Upvotes: 0

Views: 1521

Answers (1)

Julian Silden Langlo
Julian Silden Langlo

Reputation: 364

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"
    });

Upvotes: 1

Related Questions