Kis*_*nav 8 refit .net-core asp.net-core
我有一个 .Net Core Razor 页面应用程序,它尝试使用使用 Refit 创建的类库调用 .Net Core API。
我创建了一个 Refit API 接口,该接口使用以枚举作为属性类型之一的模型。
这是API端的接口片段:IPaymentAPIinterface
[Post("/recharge")]
Task<string> Recharge([Body] RechargeRequest request);
Run Code Online (Sandbox Code Playgroud)
这是请求模型: 该模型包含一个简单的 enum ELicenseType。
public class RechargeRequest
{
public ELicenseType LicenseType{ get; set; }
}
Run Code Online (Sandbox Code Playgroud)
EL 许可证类型:
public enum ELicenseType
{
NotSpecified = 0,
Standard = 1,
Commercial = 2
}
Run Code Online (Sandbox Code Playgroud)
控制器中的API实现:
[HttpPost("recharge")]
public async Task<IActionResult> Recharge(
[FromBody] RechargeRequest request)
{
Recharge result = await _mediator.Send(_mapper.Map<RechargeCommand>(request));
return Ok();
}
Run Code Online (Sandbox Code Playgroud)
调用此 Recharge 方法时,Refit 会抛出 ValidationApiException:
ValidationApiException: Response status code does not indicate success: 400 (Bad Request).
Run Code Online (Sandbox Code Playgroud)
内容是:
{
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
"title": "One or more validation errors occurred.",
"status": 400,
"traceId": "00-f9178d81421bca438241dd2def43d065-edbd7f210919b24e-00",
"errors": {
"$.licenseType": [
"The JSON value could not be converted to ELicenseType. Path: $.licenseType | LineNumber: 0 | BytePositionInLine: 98."
]
}
}
Run Code Online (Sandbox Code Playgroud)
Refit 库似乎不支持请求中的枚举,或者我的 JSON 序列化器配置错误。
小智 21
您需要在您的 ? 中设置此配置吗startup.cs?
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers().AddJsonOptions(opt =>
{
opt.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
});
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10677 次 |
| 最近记录: |