You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

[FromBody]是否总能确保参数非空?含[Required]属性的参数异常咨询

Q2: In the method public async Task<IActionResult> Method(string value, [FromBody] Person person) where Person has at least one [Required] property, and we’re seeing the error "A non-empty request body is required", can the person parameter be null?

Absolutely, and this is exactly the scenario where that happens.

That specific error message fires when the request has no request body whatsoever (or an empty one). In this case, ASP.NET Core can’t even attempt to deserialize a Person object—so the person parameter will be null. The framework catches the missing body first and returns the 400 Bad Request with that error message before it even gets to validating the [Required] properties on the Person class.

To contrast: if the client sends a valid JSON body but omits the required property, person won’t be null. You’ll get a Person instance where the required property is set to its default value (like null for a string), and the error will be related to model validation failures for that missing field—not the "non-empty request body" message.

So if you’re seeing that exact error, you can be sure person is null because no body was sent.


内容的提问来源于stack exchange,提问作者Jim Aho

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 11:22:24