我正在尝试使用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 not implemented'错误
谢谢..
答案 0 :(得分: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;
}