简体   繁体   English

Swagger/OpenAPI 静态文件未出现

[英]Swagger/OpenAPI static file not appearing

I'm trying make the dotnet 6 Weather API example generate the static swagger.json or swagger.yaml.我正在尝试使 dotnet 6 Weather API 示例生成静态 swagger.json 或 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 文档中的步骤进行操作,但没有生成静态文件,我希望它们出现在构建中。

Microsoft docs url:微软文档网址:
https://docs.microsoft.com/en-us/aspnet/core/tutorials/getting-started-with-swashbuckle?view=aspnetcore-6.0&tabs=visual-studio 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 :我的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我正在通过 Visual Studio 2022 以及dotnet run运行/构建应用程序

Change your .csproj file like below.更改您的.csproj文件,如下所示。 And please the result.并请结果。

在此处输入图像描述

<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.对于没有任何控制器或 API 的 ASP.NET 核心 Web 应用程序项目,swashbuckle 不会生成 swagger 文档。

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.如果您想在没有 API 的情况下在构建时生成 swagger 文档,那么解决方案https://stackoverflow.com/a/72654660/9247039就是您的解决方案。

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.但是如果您正在寻找在运行时生成 swagger 文档,那么您必须添加一些控制器或 API 来生成它,并且您不需要任何项目设置更改。

so with simple change in your example I was able to generated swagger.json.因此,通过对您的示例进行简单更改,我能够生成 swagger.json。 and of course I had to put swagger stuff outside this block当然我不得不把大摇大摆的东西放在这个街区之外

if (!app.Environment.IsDevelopment())

after app.MapRazorPages();在 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然后去你的招摇页面 https://localhost:7235/swagger/index.html

在此处输入图像描述

you can see your api in swagger ui.你可以在 swagger ui 中看到你的 api。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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