The easiest solution would be to place the swagger file in the wwwroot folder. You may need to create this folder since it is normally not part of a Web API.
Once the folder has been created, configure your startup process to use the StaticFiles middleware, this enables file browsing which means you can reach the file via HTTP, all you need to do then is to change the endpoint where the Swagger UI to loads the files.
For example, if I had a file in wwwroot/swagger/v1/swagger.json I would need to use the following code
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at
https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
app.UseStaticFiles();
app.UseSwagger();
// UI should use the path to the file location.
app.UseSwaggerUI(c=>c.SwaggerEndpoint("/swagger/v1/swagger.json", "v1"));
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
If dealing with YAML then you will need to use the FileExtensionContentTypeProvider as .NET only allows files with media types defined by the IANA group, but you can override this behavior with the following code.
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at
https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
var provider = new FileExtensionContentTypeProvider();
// Add new mappings
// Allow YAML files to be served via HTTP request.
provider.Mappings[".yaml"] = "text/yaml";
app.UseStaticFiles(new StaticFileOptions
{
ContentTypeProvider = provider
});
app.UseSwagger();
app.UseSwaggerUI(c=>c.SwaggerEndpoint("/swagger/v1/swagger.yaml", "v1"));
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();