简体   繁体   English

C# HttpPost-Postman Json 总是 null

[英]C# HttpPost-Postman Json always null

I'm Trying to make pass a Json to a webservice in c#.我正在尝试将 Json 传递给 c# 中的 Web 服务。 But when i pass the Json, my function in c# doesn't save it(returns null).但是当我通过 Json 时,我在 c# 中的 function 没有保存它(返回 null)。 This is my c# code.这是我的 c# 代码。

    [HttpPost]
    public JsonResult test(Product producto)
    {
        var model = producto;
        return Json(new { success = true, result = "My usuario es " + model.Name});
    }

    public class Product
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Category { get; set; }
    }

and this is what i'm sending by postman这就是我通过 postman 发送的

Postman image Postman 图像

curl --location --request POST 'http://localhost/ws/test' \
--header 'Content-Type: application/json' \
--data-raw '{"Id":1,"Name":"abc","Category":"asd"}'

Why when i'm trying to get the producto.Name returns null??为什么当我尝试获取 producto.Name 时返回 null? Thanks you very much非常感谢你

Change your action header:更改您的操作 header:


public JsonResult test([FromBody]Product producto)
{
...your code
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM