Asp.net Core Post参数始终为null [英] Asp.net Core Post parameter is always null

查看:774
本文介绍了Asp.net Core Post参数始终为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从提琴手发送POST:

I'm sending POST from fiddler:

POST http://localhost:55924/api/Product HTTP/1.1
User-Agent: Fiddler
Host: localhost:55924
Content-Type: application/json; charset=utf-8
Content-Length: 84

{"Ean″:"1122u88991″,"Name″:"Post test″,"Description":"Post test desc"}

但是Post方法始终为空.

But Post method always gets null.

// POST api/Product
[HttpPost]
public IActionResult PostProduct([FromBody]Product product)
{
    if (!ModelState.IsValid)
    {
        return BadRequest(ModelState);
    }

    _repo.Add(product);

    return CreatedAtRoute("GetToode",product);
}

当我使用[FormBody]时,产品始终为null,当不使用它时,product会被赋值,但所有字段均为null.产品类别很简单.

When I use [FormBody] product is always null, when not using it product is valued but with all fields being null. Product class is simple.

public class Product
{
    public int ProductID { get; set; }
    public string EAN { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public int? CategoryID { get; set; }
}

我尝试按照 post 但没有用.

i tried adding NullValueHandling to ConfigureServices as proposed in post but no use.

services.AddMvc()
    .AddJsonOptions(jsonOptions =>
    {
        jsonOptions.SerializerSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
    });

推荐答案

我只需要更正POST请求中的双引号就可以了.试试这个:

I just had to correct the double quotes in your POST request and it worked. Try this:

{"Ean":"1122u88991","Name":"Post test","Description":"Post test desc"}

请参见下面的屏幕截图.

See screenshot below.

这篇关于Asp.net Core Post参数始终为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆