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

ASP.NET Web API注册功能JSON数据处理报错,如何解决?

问题解决方法

核心原因

后端API接口期望直接接收RegisterRequest类型的JSON对象,但你当前的请求把数据嵌套在了registration字段里,导致后端无法正确解析出password等必填属性,触发了反序列化失败的报错。

修复步骤

修改前端请求的JSON结构,去掉外层的registration包装,直接传递包含email和password的对象:

const data = { email: 'asd@asd.bg', password:'asdfghjkj' };
const response = await fetch('https://localhost:7290/register',
        {
          method: 'POST',
          headers: { 
           'Content-Type': 'application/json',
           'Accept': '*/*' 
          },
          body: JSON.stringify(data)
        });

验证逻辑

后端的注册接口参数定义应该类似以下结构:

public class RegisterRequest
{
    [Required]
    public string Email { get; set; }
    [Required]
    public string Password { get; set; }
}

[HttpPost("register")]
public async Task<IActionResult> Register([FromBody] RegisterRequest registration)
{
    // 注册业务逻辑
}

当你直接传递{email: "...", password: "..."}格式的JSON时,后端能正确将其反序列化为RegisterRequest实例,满足必填属性的校验要求。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.20 23:04:53