400 OData请求

时间:2015-04-01 14:49:59

标签: asp.net asp.net-web-api odata .net-4.5

我使用Visual Studio 2012创建了一个.NET 4.5 Web项目,以便在this tutorial之后使用OData进行测试。但是,每当我从Fiddler发送以下请求时:

PUT http://localhost:14116/odata/Products(5) HTTP/1.1
Host: localhost:14116
Accept: application/json
Content-Type: application/json
Content-Length: 67

{ "Name" : "Apple //e", "Price" : "1298", "Category" : "Computer" }

我收到以下回复:

HTTP/1.1 400 Bad Request
Cache-Control: no-cache
Pragma: no-cache
Expires: -1
Server: Microsoft-IIS/8.0
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?YzpcdXNlcnNcZGxhdmVsbGVcZG9jdW1lbnRzXHZpc3VhbCBzdHVkaW8gMjAxMlxQcm9qZWN0c1xP    RGF0YVRlc3QzXE9EYXRhVGVzdDNcb2RhdGFcUHJvZHVjdHMoNSk=?=
X-Powered-By: ASP.NET
Date: Wed, 01 Apr 2015 14:37:41 GMT
Content-Length: 0

我错过了什么? (堆栈溢出正在HTTP响应中删除两个空行。)

此请求正常:

GET http://localhost:14116/odata/Products?$filter=Price+lt+500 HTTP/1.1
Host: localhost:14116
Accept: application/json

这是我的控制器:

public class ProductsController : ODataController
{
    static List<Product> s_products;
    static ProductsController()
    {
        s_products = new List<Product>();
        s_products.Add(
            new Product
            {
                Id = 1
              , Name = "Nexus 7"
              , Price = 179
              , Category = "Tablet"
            }
        );
    }

    // GET odata/Products
    [Queryable]
    public IQueryable<Product> GetProducts()
    {
        return s_products.AsQueryable();
    }

    // GET odata/Products(5)
    [Queryable]
    public SingleResult<Product> GetProduct([FromODataUri] int key)
    {
        return SingleResult.Create(s_products.AsQueryable().Where(product => product.Id == key));
    }

    // PUT odata/Products(5)
    public IHttpActionResult Put([FromODataUri] int key, Product product)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        if (key != product.Id)
        {
            return BadRequest();
        }

        var previous = s_products.SingleOrDefault(p => p.Id == key);
        if (previous == null)
        {
            return NotFound();
        }

        previous.Category = product.Category;
        previous.Name = product.Name;
        previous.Price = product.Price;

        return Updated(product);
    }

    // POST odata/Products
    public IHttpActionResult Post(Product product)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        s_products.Add(product);

        return Created(product);
    }

    // PATCH odata/Products(5)
    [AcceptVerbs("PATCH", "MERGE")]
    public IHttpActionResult Patch([FromODataUri] int key, Delta<Product> patch)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        Product product = s_products.SingleOrDefault(p => p.Id == key);
        if (product == null)
        {
            return NotFound();
        }
        patch.Patch(product);

        return Updated(product);
    }

    // DELETE odata/Products(5)
    public IHttpActionResult Delete([FromODataUri] int key)
    {
        Product product = s_products.SingleOrDefault(p => p.Id == key);
        if (product == null)
        {
            return NotFound();
        }

        s_products.Remove(product);

        return StatusCode(HttpStatusCode.NoContent);
    }

    protected override void Dispose(bool disposing)
    {
        base.Dispose(disposing);
    }
}

0 个答案:

没有答案