Web API 2不处理整数的PATCH请求 [英] Web API 2 does not process PATCH requests for Integers

查看:220
本文介绍了Web API 2不处理整数的PATCH请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到Web API 2(.net 4.5.1)的问题,因为它似乎忽略了属性为整数的PATCH请求,但是处理其他类型没有问题(我测试了字符串和小数) )。

I'm having a problem with Web API 2 (.net 4.5.1) in that it seems to ignore PATCH requests where the property is an integer, but processes other types without a problem (I've tested string and decimal).

我在 http://playapi.azurewebsites.net/api/products 。如果您对该网址进行GET,您将获得类似此产品的内容:

I’ve setup an unsecured test API with a 'products' controller at http://playapi.azurewebsites.net/api/products. If you do a GET to that URL, you’ll get something like this product back:

{"Id": 1,"Name": "Xbox One","Category": "gaming","Price": 300,"Stock": 5}

'名称'和'类别'都是字符串,'Price'是十进制,'Stock'是整数。

‘Name’ and ‘Category’ are both strings, ‘Price’ is a Decimal and ‘Stock’ is an Integer.

如果你发送这些请求,他们都工作(你将获得200 / OK与更新的实体):

If you send these requests, they both work (You’ll get a 200/OK with the updated entity):

  • PATCH, http://playapi.azurewebsites.net/api/products/1 with {"Price": 600.00}
  • PATCH, http://playapi.azurewebsites.net/api/products/1 with {"Category": "Electronics"}

但是,如果您发送此信息,则返回200 / OK,但不进行更新,并且库存保持原始值

However, if you send this, it returns 200/OK, but does not make the update and the stock remains at the original value

  • PATCH, http://playapi.azurewebsites.net/api/products/1 with {"Stock": 4}

我的控制器代码是相当标准的锅炉板代码(来自脚手架ODATA控制器,但移入标准API控制器):

My controller code is fairly standard boiler plate code (from the scaffolded ODATA controller but moved into a standard API controller):

// PATCH: api/Products/5
[AcceptVerbs("PATCH", "MERGE")]
public async Task<IHttpActionResult> PatchOrder(int id, Delta<Product> patch)
{
    Validate(patch.GetEntity());
    if (!ModelState.IsValid)
    {
        return BadRequest(ModelState);
    }
    var item = await db.Products.FindAsync(id);
    if (item == null)
    {
        return NotFound();
    }
    patch.Patch(item);
    try
    {
        await db.SaveChangesAsync();
    }
    catch (DbUpdateConcurrencyException)
    {
        if (!ProductExists(id))
        {
            return NotFound();
        }
        else
        {
            throw;
        }
    }
    return Ok(item);
}

我的'产品'模型如下:

My model for 'Product' is as follows:

namespace PlayAPI.Models
{
    public class Product
    {
        [Key]
        public int Id { get; set; }
        public string Name { get; set; }
        public string Category { get; set; }
        public double Price { get; set; }
        public int Stock { get; set; }
    }
}

当我调试控制器时,我看到了'patch'对象有一个 _changedProperties 集合,当我执行整数请求时,它没有任何项目,但是当我做任何其他类型的请求时,它具有我更改的密钥。

When I debug the controller, I see that the ‘patch’ object has a _changedProperties collection which has no items in it when I do an integer request, but when I do any other kind of request it has the key that I changed.

Web API是否应支持整数属性的PATCH请求?如果是这样,我是否需要在服务器或客户端上执行任何特殊操作才能使其正常工作?

Should web API support PATCH requests for integer properties? If so, do I need to do anything special on the server or client to make it work?

推荐答案

作为快速解决方案,在PlayAPI.Models.Product上将int更改为Int64。

As a quick fix, Change the int to an Int64 on PlayAPI.Models.Product.

public Int64 Stock { get; set; }

我的理解是,用于修补现有对象的Delta对象不使用JSON.net转换并在解析JSON时静默抛出无效的强制转换异常,然后与数据库中的现有对象进行比较。您可以在此处详细了解该错误: http://aspnetwebstack.codeplex.com/workitem/777

It's my understanding that The Delta object used to patch the existing object doesn’t use JSON.net to convert and is silently throwing an Invalid cast exception when it parses JSON and then compares to the existing object from your database. You can read more about the bug over here: http://aspnetwebstack.codeplex.com/workitem/777

这篇关于Web API 2不处理整数的PATCH请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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