'How to inject openapi specification file with Swashbuckle.AspNetCore

services.AddSwaggerGen(x =>
{
                
});

I have explored the AddSwaggerGen() method of NuGet Swashbuckle.AspNetCore. Unfortunately, I haven't found a way to use the path of the already generated open API specification file.

My APIs are related to file upload in .net core and I have already written the swagger specification file swagger.yaml and It is working fine with editor.swagger.io. Can anyone help me? How can I inject the generated open API specification file and use it with SwaggerUI in .net core with NuGet Swashbuckle.AspNetCore?

app.UseSwaggerUI(c =>
{
  c.SwaggerEndpoint("/swagger/swagger.yaml", "My API V1");
});

I have created a static file /swagger/swagger.yaml in my project But Swagger UI still using the same file which is generated by internally.



Solution 1:[1]

I also stumbled upon this. One working solution is to host open-api/swagger file in "wwwroot/swagger/swagger.yaml" and use the static file hosting middleware. You just need the SwaggerUI dependency, nothing more.

After that, this works.

app.UseSwaggerUI(c =>
{
  c.SwaggerEndpoint("/swagger/swagger.yaml", "My API V1");
});

Unfortunately, I encountered the issue that the .NET core doesn't host static YAML files by default. You can solve this by either configuring FileExtensionContentTypeProvider or enabling serve unknown content types.

Hope that this helps.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Marko Andersson