问题描述
将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
}
}
GetPersonInput是:
public class GetPersonInput
{
[FromQuery]
public IdentityType? IdentityType { get; set; }
[FromRoute]
public string IdentityNumber { get; set; }
}
当我从Postman调用它时,它工作正常。但是,当我从Swagger UI调用它时,它很好地识别了方法签名,它将/{IdentityNumber}
传递给URI,而不是我在UI中输入的数字:
实际电话是(来自邮递员): https:// localhost:44343 / api / person / 25062140?identityType = DU
但是醒目的UI会建立这种卷曲:
curl -X GET "https://localhost:44343/api/person/{identityNumber}?identityType=DU" -H "accept: application/json"
产生此请求:
https://localhost:44343/api/person/{identityNumber}?identityType=DU
这会导致异常:
System.FormatException: Input string was not in a correct format.
这是我的启动配置
public void ConfigureServices(IServiceCollection services)
{
//Add routing
services.AddRouting(options =>
{
options.LowercaseUrls = true;
});
services.AddControllers();
//services.AddControllers().SetCompatibilityVersion(CompatibilityVersion.Version_2_2); //The problem also happens with CompatiblityVersion set to 2.2
//Add OpenApi
services.AddOpenApiDocument();
services.AddApiVersioning(options =>
{
options.DefaultApiVersion = new ApiVersion(1,status: "dev");
options.ApiVersionReader = new QueryStringApiVersionReader();
options.AssumeDefaultVersionWhenUnspecified = true;
options.ReportApiVersions = true;
options.ApiVersionSelector = new CurrentImplementationApiVersionSelector(options);
});
}
public void Configure(IApplicationBuilder app,IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHsts();
}
app.UseHttpsRedirection();
//Use OpenApi with UIs
app.UseOpenApi();
app.UseSwaggerUi3();
//app.UseReDoc();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)