- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试了解如何拦截和处理 .net Core 中的模型绑定(bind)错误。
我想这样做:
// POST api/values
[HttpPost]
public void Post([FromBody] Thing value)
{
if (!ModelState.IsValid)
{
// Handle Error Here
}
}
“事物”的模型在哪里:
public class Thing
{
public string Description { get; set; }
public int Amount { get; set; }
}
但是,如果我传入无效金额,例如:
{
"description" : "Cats",
"amount" : 21.25
}
我收到这样的错误:
{"amount":["Input string '21.25' is not a valid integer. Path 'amount', line 1, position 38."]}
没有 Controller 代码被命中。
如何自定义发回的错误? (基本上我需要将这个序列化错误包装在一个更大的错误对象中)
最佳答案
所以,我之前错过了这个,但我在这里找到了:
如果你使用
[ApiController]
Controller 上的属性,它会自动处理序列化错误并提供 400 响应,相当于:
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
您可以像这样在 Startup.cs 中关闭此行为:
services.AddMvc()
.ConfigureApiBehaviorOptions(options =>
{
options.SuppressModelStateInvalidFilter = true;
});
如果您希望自定义响应,更好的选择是使用 InvalidModelStateResponseFactory,它是一个采用 ActionContext 并返回将被调用以处理序列化错误的 IActionResult 的委托(delegate)。
看这个例子:
services.Configure<ApiBehaviorOptions>(options =>
{
options.InvalidModelStateResponseFactory = actionContext =>
{
var errors = actionContext.ModelState
.Where(e => e.Value.Errors.Count > 0)
.Select(e => new Error
{
Name = e.Key,
Message = e.Value.Errors.First().ErrorMessage
}).ToArray();
return new BadRequestObjectResult(errors);
}
});
关于c# - 在 .NET Core 2.1 中使用 [FromBody] 时处理模型绑定(bind)错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55100471/
这是基本设置,我有一个 asp.net core webapi Controller (在 C# 中),具有如下的 post 函数: [HttpPost] public ActionResult Po
这是基本设置,我有一个 asp.net 核心 webapi Controller (在 c# 中),带有一个像这样的 post 函数: [HttpPost] public ActionResult P
这是 Asp.Net Webform 应用程序 这是我的 Apicontroller 中的 POST 方法 public void Post([FromBody]string value) { } 我
这可能是非常基本的东西,但我无法弄清楚我哪里出错了。 我试图从 POST 正文中获取字符串,但“jsonString”仅显示为 null。我也想避免使用模型,但也许这是不可能的。我用 PostMan
在 Web API (2) 中使用属性路由时,我希望能够自动将路由参数从 URL 获取到模型参数中。这样做的原因是我的验证是在它到达操作之前在过滤器中执行的,如果没有它,附加信息就不容易获得。 考虑以
我收到了这样的请求。 POST /API/Event?EventID=15&UserID=1&Severity=5&DeptID=1&FlowTag=CE HTTP/1.1 Content-Type:
在 ASP.NET Core(本例中为 2.1)的 Controller 中使用 [FromBody] 时,它会处理所有到指定模型的转换并确保模型有效。这意味着如果提供的输入格式错误, Control
我正在尝试传递一个包含不同数据类型的对象。我总是在 Web API 中获取 orderDetails 的空值。 但是如果这样做, purchaseOrder.Attachments = null, 然
我有一个使用 ASP.NET Core 构建的新 API,但我无法将任何数据发布到端点。 这是端点的样子: [HttpPost] [Route("StudentResults")] public as
有没有[FromBody]属性的作用?我的意思是,当我使用它时: public async Task SetUser([FromBody]User 用户) 当我使用时: public async Ta
我似乎遇到了一个有趣的 Web API 问题 - 使用这个 Controller 操作,在左大括号上设置断点来检查 thing: [HttpPut] public void Test([FromBod
这个问题在这里已经有了答案: Web api attribute routing, by content type, in dot net core 2? (1 个回答) 关闭 4 年前。 客户端可
RESTful 设计更喜欢 URL 或 [FromBody] 中的数据吗? 假设我有一个 Controller 方法: [Route("ServiceActivity({serviceActivity
我有一个应该是非常基本的场景......我觉得我之前已经做过 100 次但由于某种原因它决定不工作并将我的时间浪费在一些琐碎的事情上...... 我正在使用带有属性路由的 WebAPI(喜欢它们)。
您好,我想通过我在 Web API Controller 中的 body 变量从 C# 客户端调用 Web Api 方法一直为空。如何正确设置? 客户端: IFileService imgServic
我在 web api 中有一个新方法 [HttpPost] public ApiResponse PushMessage( [FromUri] string x, [FromUri] string y
我有一个类似于 ASP.NET MVC 4 RC Web API Parameter Binding Issue 的问题,但我正在尝试使用 [FromBody] 属性来解决它。 Fiddler 报告以
我的 body 参数始终为 null 时遇到问题。这是 Controller : [HttpPost] public async void Assign([FromUri] int[]
我可以将我的数据发送到服务器,但只有当我使用 FromBody-Attribute 时。 为什么没有使用 Post 自动从 Body 中读取 json 数据? 后端网页接口(interface) [H
当我尝试发送请求“http://localhost:1234/api/case/create?signature=123456”时来自 Postman(谷歌扩展)在正文请求中使用“表单数据”,我收到错
我是一名优秀的程序员,十分优秀!