Swagger/Openapi静态文件未出现

发布于 2025-02-07 19:55:54 字数 1196 浏览 1 评论 0 原文

我正在尝试使dotnet 6天气API示例产生静态摇晃。json或Swagger.yaml。

我很确定我已经遵循了Microsoft文档的步骤,但是没有生成静态文件,我希望它们会出现在构建中。

Microsoft文档URL:
https://learn.microsoft.com/en-us/aspnet/core/core/tutorials/getting-started-with-with-with-swashbuckle?view=

asspnetcore-6.0&tabs = visual-studio 我的 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();

我正在通过Visual Studio 2022以及 dotnet Run 来运行/构建应用程序

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://learn.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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

戒ㄋ 2025-02-14 19:55:54

如下所示,更改您的 .csproj 文件。并请结果。

<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>

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>
↙厌世 2025-02-14 19:55:54

对于没有任何控制器或API的ASP.NET Core Web应用程序项目,将不会生成Swagger文档。

因此,它的一点取决于您要实现的目标。

如果您想在没有API的构建时间生成Swagger Document 是您的解决方案。

但是,如果您在运行时正在寻找生成Swagger文档,则必须添加一些控制器或API才能生成,并且您将不需要任何项目设置更改。

因此,在您的示例中,我可以生成Swagger.json。当然,

if (!app.Environment.IsDevelopment())

在App.maprazorpages()之后,我不得不将摇晃的东西放在此块之外。添加这些行并运行应用程序。

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

然后转到您的招摇页面
https:// localhost:7235/swagger/index.html

“在此处输入图像说明”

您可以在Swagger UI中看到您的API。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文