RESTful Unity 中的客户端 - 验证错误

RESTful client in Unity - validation error

我有一个使用 ASP.Net 创建的 RESTful 服务器,我正在尝试使用来自 Unity 的 RESTful 客户端连接到它。 GET 工作完美,但是我在发送 POST 请求时收到验证错误。同时 GET 和 POST 在从 Postman 发送请求时工作。 我的服务器:

        [HttpPost]
        public IActionResult Create(User user){
            Console.WriteLine("***POST***");
            Console.WriteLine(user.Id+", "+user.sex+", "+user.age);
            if(!ModelState.IsValid)
                return BadRequest(ModelState);
            _context.Users.Add(user);
            _context.SaveChanges();
            return CreatedAtRoute("GetUser", new { id = user.Id }, user);
        }

我的客户:

IEnumerator PostRequest(string uri, User user){
        string u = JsonUtility.ToJson(user);
        Debug.Log(u);
        using (UnityWebRequest webRequest = UnityWebRequest.Post(uri, u)){
            webRequest.SetRequestHeader("Content-Type","application/json");
            yield return webRequest.SendWebRequest();
            string[] pages = uri.Split('/');
            int page = pages.Length - 1;
            if (webRequest.isNetworkError || webRequest.isHttpError){
                Debug.Log(pages[page] + ":\nReceived: " + webRequest.downloadHandler.text);
            }
            else{
                Debug.Log(pages[page] + ":\nReceived: " + webRequest.downloadHandler.text);
            }
        }
    }

我尝试使用 Json 转换并自己编写字符串,也尝试使用 WWWForm,但错误仍然存​​在。
该错误表明这是一个未知的 HTTP 错误。打印返回的文本时,它说:
"One or more validation errors occurred.","status":400,"traceId":"|b95d39b7-4b773429a8f72b3c.","errors":{"$":["'%' is an invalid start of a value. Path: $ | LineNumber: 0 | BytePositionInLine: 0."]}}
在服务器端,它识别出正确的方法和控制器,但是,它甚至没有到达方法的第一行 (Console.WriteLine)。然后它说:"Executing ObjectResult, writing value of type 'Microsoft.AspNetCore.Mvc.ValidationProblemDetails'"。 以下是所有服务器端消息:

info: Microsoft.AspNetCore.Hosting.Diagnostics[1]
      Request starting HTTP/1.1 POST http://localhost:5001/user application/json 53
info: Microsoft.AspNetCore.Routing.EndpointMiddleware[0]
      Executing endpoint 'TheNewestDbConnect.Controllers.UserController.Create (TheNewestDbConnect)'
info: Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker[3]
      Route matched with {action = "Create", controller = "User"}. Executing controller action with signature Microsoft.AspNetCore.Mvc.IActionResult Create(TheNewestDbConnect.Data.Entities.User) on controller TheNewestDbConnect.Controllers.UserController (TheNewestDbConnect).
info: Microsoft.AspNetCore.Mvc.Infrastructure.ObjectResultExecutor[1]
      Executing ObjectResult, writing value of type 'Microsoft.AspNetCore.Mvc.ValidationProblemDetails'.
info: Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker[2]
      Executed action TheNewestDbConnect.Controllers.UserController.Create (TheNewestDbConnect) in 6.680400000000001ms
info: Microsoft.AspNetCore.Routing.EndpointMiddleware[1]
      Executed endpoint 'TheNewestDbConnect.Controllers.UserController.Create (TheNewestDbConnect)'
info: Microsoft.AspNetCore.Hosting.Diagnostics[2]
      Request finished in 11.3971ms 400 application/problem+json; charset=utf-8
info: Microsoft.AspNetCore.Hosting.Diagnostics[1]

我不知道发生了什么,也不知道如何解决。非常感谢任何帮助!

原来我只是缺少一个上传处理程序。添加这一行解决了它: webRequest.uploadHandler = new UploadHandlerRaw(System.Text.Encoding.UTF8.GetBytes(JsonObject));