简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM