2

我正在尝试使用 OData 将数据发送到 Web API 并能够调试 Web API 控制器方法,但是在返回模型 Web API 时抛出错误“501 错误代码”

我有这个使用 JQuery 的电话:

  OData.request(
          {
              requestUri: "/odata/Products",
              method: "POST",
              data: {
                  ID:"22",
                  Name : "Hat1", 
                  Price: 122, 
                  Category : "Apparel" 
              }
          },
          function (data) {
              alert(data.Price);
          });

我的控制器是:

public class ProductsController : EntitySetController<Product, int>
    {
        ProductsContext _context = new ProductsContext();

        [Queryable]
        public override IQueryable<Product> Get()
        {
            return _context.Products;
        }

        protected override int GetKey(Product entity)
        {
            return entity.ID;
        }

        protected override Product GetEntityByKey(int key)
        {
            return _context.Products.FirstOrDefault(p => p.ID == key);
        }

        protected override Product CreateEntity(Product entity)
        {           

            return entity;
        }
}

CreateEntity方法工作正常,但在控制台日志中抛出“501 未实现”错误

谢谢..

4

1 回答 1

1

我试过用 HttpResponseMessage 发帖,它奏效了:)

public override HttpResponseMessage Post(UserEntity user)
            {


                var response = Request.CreateResponse(
                    HttpStatusCode.Created,
               `enter code here`     user);
                response.Headers.Location = new Uri(Url.ODataLink(
                    new EntitySetPathSegment("User"),
                    new KeyValuePathSegment(user.UserID.ToString())));
                return response;
            }
于 2013-08-20T11:40:03.173 回答