【发布时间】:2021-06-09 09:20:44
【问题描述】:
我正在尝试熟悉 .NET 5 as documented here 的 NSwag 用法。阅读后,我觉得这个文档已经过时了,因为它提到了诸如 UseMvc 和 AddMvc 之类的方法,这些方法是 .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”为我的客户项目生成打字稿代码。
【问题讨论】:
-
能否贴出
WeatherForecastController及其GET方法的代码和注解? -
@Helen 是 Visual Studio 2019 自动生成的标准项目。也可以在这里找到:github.com/dotnet/AspNetCore.Docs/blob/main/aspnetcore/…
标签: asp.net-core .net-5 swagger-codegen nswag asp.net5