简体   繁体   English

获取 JSON 值的 POST 请求无法转换为 System.String。 路径错误

[英]POST request getting JSON value could not be converted to System.String. Path error

I'm making a POST request to API endpoint http://localhost:20779/api/Account and getting the following error.我正在向 API 端点http://localhost:20779/api/Account发出 POST 请求并收到以下错误。

The JSON value could not be converted to System.String.无法将 JSON 值转换为 System.String。 Path: $ |路径:$ | LineNumber: 0 |行号:0 | BytePositionInLine: 1." BytePositionInLine:1。

This is the JSON I'm using to make POST request.这是我用来发出 POST 请求的 JSON。

{
    "EmailAddress": "hello@example.com",
    "Username": "example",
    "Password": "mypassword",
    "Status": "A",
    "CreatedOn": "2021-08-10 08:00:00"
}

The request hits the POST method.请求命中 POST 方法。

namespace HelpDesk.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class AccountController : ControllerBase
    {
        // POST api/<AccountController>
        [HttpPost]
        public void Post([FromBody] string value)
        {
            var x = string.Empty;
        }
  
    }
}

Try to change [FromBody] string value to [FromBody] object content and then if you want to read as string use content.ToString()尝试将[FromBody] string value更改为[FromBody] object content ,然后如果要读取为字符串,请使用content.ToString()

you have to create a view model class你必须创建一个视图模型类

public UserViewModel
{
public string EmailAddress { get; set; }
public string Username { get; set; }
public string Password { get; set; }
public string Status { get; set; }
public DateTime CreatedOn { get; set; }
}

and fix the action并修复动作

 public IActionResult Post([FromBody] UserViewModel model)
{
            return Ok(model.Username);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JSON 值无法转换为 System.String。 路径:$[0].Price | 行号:4 | 字节位置行内:16 - The JSON value could not be converted to System.String. Path: $[0].Price | LineNumber: 4 | BytePositionInLine: 16 ASP.NET Core 3.0 [FromBody] 字符串内容返回“无法将 JSON 值转换为 System.String。” - ASP.NET Core 3.0 [FromBody] string content returns "The JSON value could not be converted to System.String." 反序列化对象数组:无法将 JSON 值转换为 System.String[] - Deserializing an array of objects: The JSON value could not be converted to System.String[] JSON 值无法转换为 System.Byte[] - The JSON value could not be converted to System.Byte[] System.Text.Json.JsonException:无法转换 JSON 值 - System.Text.Json.JsonException: The JSON value could not be converted 尝试调用控制器端点时“无法将 JSON 值转换为 System.String” - “The JSON value could not be converted to System.String” when attempting to call controller endpoint JsonSerializer.Deserialize 异常:JSON 值无法转换为 System.String - JsonSerializer.Deserialize exception: The JSON value could not be converted to System.String POST 响应给出错误:“JSON 值无法转换为 api.Models.Account” - POST response gives error: “The JSON value could not be converted to api.Models.Account” JSON 值无法转换为 System.Int32 - The JSON value could not be converted to System.Int32 无法将 JSON 值转换为 System.Collections.Generic.List - The JSON value could not be converted to System.Collections.Generic.List
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM