在 Asp.Net Core v3.1 api、Swagger v3.0 中,当我声明了多个版本时,Swagger UI 无法加载 API 定义。
编辑:同样来自 NuGet:
Mcrosoft.AspNetCore.Mvc.版本控制 v4.1.1
NSwag.AspNetCore v13.7.0
根据 NSwag 的文档,我意识到我必须将以下内容添加到我的“Startup.ConfigureServices()”中:
services.AddApiVersioning(options =>
{
options.AssumeDefaultVersionWhenUnspecified = true;
options.ApiVersionReader = new UrlSegmentApiVersionReader();
})
.AddMvcCore()
.AddVersionedApiExplorer(options =>
{
options.GroupNameFormat = "VVV";
options.SubstituteApiVersionInUrl = true;
});
Run Code Online (Sandbox Code Playgroud)
但是 '''AddVersionedApiExplore()''' 在那里不可用...然后我发现这个 ApiExplorerOptions wiki其中指出(我是这样理解的)自 Asp.Net Core 3.0 以来,它的用法是:
services.AddVersionedApiExplorer( options => { /* configure options */ } );
Run Code Online (Sandbox Code Playgroud)
但我最终得到的错误'IServiceCollection'不包含定义'AddVersionedApiExplorer'
顺便说一句,每个版本的 Postman 服务的所有路径都运行良好,swagger 的页面加载并显示我声明的两个版本,但无法加载它们的定义。
有人可以指出我正确的方向吗?
将 ASP.Net Core 3.1 与 NuGet 中的 NSwag.AspNetCore v13.7.0 结合使用
我有这个动作方法
[ApiController]
[ApiVersion(EosApiVersion.CurrentVersion)]
[Route(EosApiConsts.BaseRoute + "/[Controller]")]
public class PartyController : ControllerBase
{
[HttpGet("{identityNumber}", Name = nameof(GetPersonById))]
[ApiConventionMethod(typeof(DefaultApiConventions), nameof(DefaultApiConventions.Get))]
public ActionResult<PersonResource> GetPersonById([FromQuery] GetPersonInput input)
{
//code omitted for brevity
}
}
Run Code Online (Sandbox Code Playgroud)
GetPersonInput 是:
public class GetPersonInput
{
[FromQuery]
public IdentityType? IdentityType { get; set; }
[FromRoute]
public string IdentityNumber { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
当我从邮递员调用它时,它工作正常。但是当我从 Swagger UI 调用它时,它很好地识别了方法签名,它传递/{IdentityNumber}到 URI 而不是我在 UI 中输入的数字:
实际调用是(来自 Postman): https://localhost:44343/api/person/25062140?identityType=DU
但是 swagger UI 构建了这个curl: …