Swagger/OpenAPI static file not appearing

Viewed 287

I'm trying make the dotnet 6 Weather API example generate the static swagger.json or swagger.yaml.

I'm pretty sure i've followed the steps from Microsoft docs but no static files are being generated, i'm expected them to appear on build.

Microsoft docs url:
https://docs.microsoft.com/en-us/aspnet/core/tutorials/getting-started-with-swashbuckle?view=aspnetcore-6.0&tabs=visual-studio

The code in my program.cs:

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddRazorPages();
builder.Services.AddControllersWithViews();

builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Error");
    app.UseHsts();

    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseAuthorization();

app.MapDefaultControllerRoute();
app.MapRazorPages();

app.Run();

I'm running/building the app through visual studio 2022 as well as dotnet run

2 Answers

Change your .csproj file like below. And please the result.

enter image description here

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Swashbuckle.AspNetCore" Version="6.3.1" />
    <PackageReference Include="Swashbuckle.AspNetCore.Swagger" Version="6.3.1" />
    <PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="6.3.1" />
    <PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="6.3.1" />
  </ItemGroup>
  <Target Name="PostBuild" AfterTargets="PostBuildEvent">
    <Exec Command="dotnet tool restore" />
    <Exec Command="dotnet new tool-manifest --force" />
    <Exec Command="dotnet tool install --local Swashbuckle.AspNetCore.Cli"/>
    <Exec Command="dotnet swagger tofile --output swagger.json $(OutputPath)\$(AssemblyName).dll v1 " />
</Target>
</Project>

For a ASP.NET core web app project which does not have any controller or APIs swashbuckle will not generate the swagger document.

So its little bit depends on what you want to achieve.

if you want to generate swagger document at the build time with no APIs then the solution https://stackoverflow.com/a/72654660/9247039 is the solution for you.

but if you are looking for generating swagger document during runtime then you have to add some controller or APIs to get it generated and you will not need any project setting changes.

so with simple change in your example I was able to generated swagger.json. and of course I had to put swagger stuff outside this block

if (!app.Environment.IsDevelopment())

after app.MapRazorPages(); add these lines and run the application.

app.MapGet("/hello", () =>
{
    return "Hello World";
});

and then go your swagger page https://localhost:7235/swagger/index.html

enter image description here

you can see your api in swagger ui.

Related