小编Bob*_*Ash的帖子

.net 核心 API 中的无效模型类属性错误

当我从邮递员向 .net 核心 API 发送具有无效模型类属性的 post 方法时,例如模型类包含长字段但我发送字符串值,我在邮递员中收到错误,但在 catch 方法中没有收到错误

    [HttpPost]
    public async Task<IActionResult> CalculateFee(CollegeGetModel collegeModel)
    {
        try
        {
            return await _collegeService.CalculateFee(collegeModel);
        }
        catch (Exception ex)
        {
            return ex.Message;
        }
    }
Run Code Online (Sandbox Code Playgroud)

和模型类是

public class CollegeGetModel {
   public long Id {get;set;}
   public string Name {get;set;}
}
Run Code Online (Sandbox Code Playgroud)

返回的错误信息是

{
"errors": {
    "Id": [
        "Error converting value \"str\" to type 'System.Int64'. Path 'Id', line 2, position 20."
    ]
},
"title": "One or more validation errors occurred.",
"status": 400,
"traceId": "0HLTA1MNU7LV5:00000001"
}
Run Code Online (Sandbox Code Playgroud)

我没有在控制器捕获方法中收到此错误消息。如何在控制器方法中获取此错误消息?

c# asp.net-core-webapi

3
推荐指数
1
解决办法
1549
查看次数

标签 统计

asp.net-core-webapi ×1

c# ×1