【问题标题】:Web API Parameters are always NullWeb API 参数始终为 Null
【发布时间】:2017-03-04 06:03:54
【问题描述】:

我试图找出为什么我不能将 Json 字符串传递给我的 Rest API。

我在 VS 2015 中使用 WebAPI 和 Entityframework 创建了这个项目。 Odata V3 控制器和实体类由 VS 自动生成。 GET 和 DELETE 方法工作正常。但是更新和添加方法不起作用,因为我无法将我的 Json 对象传递给我的 rest api post 和 put 方法。参数始终为空。

这是我自动生成的客户实体类。

 public partial class Customer
{
    public int CustomerID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Address { get; set; }
    public string PhoneNumber { get; set; }
    public string Email { get; set; }
}

这是我的控制器

public IHttpActionResult Post(Customer customer)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        db.Customers.Add(customer);
        db.SaveChanges();

        return Created(customer);
    }

    // PUT: odata/Customers(5)
    public IHttpActionResult Put([FromODataUri] int key, Delta<Customer>     patch)
    {
        Validate(patch.GetEntity());

        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        Customer customer = db.Customers.Find(key);
        if (customer == null)
        {
            return NotFound();
        }

        patch.Put(customer);

        try
        {
            db.SaveChanges();
        }
        catch (DbUpdateConcurrencyException)
        {
            if (!CustomerExists(key))
            {
                return NotFound();
            }
            else
            {
                throw;
            }
        }

        return Updated(customer);
    }

这是我的 Jquery 调用

var customerData = {
        CustomerID: "0",
        FirstName: FirstName,
        LastName: LastName,
        Address: Address,
        PhoneNumber: PhoneNumber,
        Email: Email
    };

    var requestBody = JSON.stringify(customerData);
    return $.ajax({
        url: requestUri,
        type: "POST",                  
        data: requestBody
    });

这是我的提琴手

POST http://localhost:1360/odata/Customers/ HTTP/1.1
Accept: application/json, text/javascript, */*; q=0.01
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
X-Requested-With: XMLHttpRequest
Referer: http://localhost:1360/Pages/Customers.aspx
Accept-Language: en-US
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko

连接:保持活动状态 内容长度:101 主机:本地主机:1360 Pragma: 无缓存

 {"CustomerID":0,"FirstName":"wer","LastName":"wer","Address":"wer","PhoneNumber":"wer","Email":"wer"}

对其他人有用的相关帖子都不对我有用:

Receiving JSON-formatted data in a Web API 2 action?

jQuery AJAX post data is null in c# web api controller

提前致谢。请让我知道代码有什么问题以及如何修复它。

【问题讨论】:

  • 有一段时间没有使用 odata3,但我认为 put/post 不会使用 delta,只有补丁操作会。如果您在方法签名中尝试使用 Customer 而不是 Delta 会怎样?
  • 此代码由 VS 自动生成。虽然 post 方法在签名中只有客户,但我在调试时得到空值。虽然发送了 json 字符串。服务器无法反序列化它。

标签: jquery json rest odata


【解决方案1】:

我发现了问题。我应该在我的 Ajax 调用中添加 contentType:"application/json"。一旦我更新了我的请求,代码就开始工作了。

谢谢希望这可能会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-26
    • 2013-01-15
    • 1970-01-01
    • 2020-03-11
    • 2017-08-09
    • 2014-08-06
    • 2020-11-06
    • 1970-01-01
    相关资源
    最近更新 更多