【问题标题】:How to show the full swagger interface when using NSwag with .NET 5?将 NSwag 与 .NET 5 一起使用时如何显示完整的招摇界面?
【发布时间】:2021-06-09 09:20:44
【问题描述】:

我正在尝试熟悉 .NET 5 as documented here 的 NSwag 用法。阅读后,我觉得这个文档已经过时了,因为它提到了诸如 UseMvcAddMvc 之类的方法,这些方法是 .NET Core 2.1 的典型方法,但不适用于 .NET 5。无论如何,这是我的配置方法(我操纵Visual Studio 2019 自动生成的标准 .NET 5 Web Api 项目):

public void ConfigureServices(IServiceCollection services)
{
    services.AddControllers();
    services.AddSwaggerDocument();
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    app.UseOpenApi();
    app.UseSwaggerUi3();
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }

    app.UseRouting();

    app.UseAuthorization();

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllers();
    });
}

按F5后显示如下:

这是项目文件:

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

  <PropertyGroup>
    <TargetFramework>net5.0</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="NSwag.AspNetCore" Version="13.11.3" />
    <PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3" />
  </ItemGroup>

</Project>

显然,我“丢失了”WeatherForecastController 的 GET 方法。那么,如何在 .NET 5 中使用 NSwag 时显示完整的 swagger 界面? The code examples referred to by the docs,也对我没有帮助,因为它们基于旧版本的 .NET Core。我使用 NSwag 的原因是“the most popular tool for the job”为我的客户项目生成打字稿代码。

【问题讨论】:

标签: asp.net-core .net-5 swagger-codegen nswag asp.net5


【解决方案1】:

点击 WeatherForecast 展开节点查看详情,您会在此处找到 GET 方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-12
    • 2019-07-15
    • 2021-12-14
    • 2021-09-23
    • 1970-01-01
    • 1970-01-01
    • 2020-07-07
    相关资源
    最近更新 更多