我正在尝试删除一个对象并返回一个列表,该列表会触发web api控制器方法但会出现错误
Expected response to contain an object but got an array (Request: DELETE
$scope.deleteProduct = function (productId) {
productResource.delete({
id: productId
}, function (data) {
$scope.products = data;
});
}
资源控制器
function productResource($resource) {
return $resource("/api/products/:id");
}
Web api控制器
public IQueryable Delete(int id)
{
var repository = new ProductRepository();
return repository.Delete(id).AsQueryable();
}
这是对数据库的调用,返回产品列表。
internal List<Product> Delete(int Id)
{
IDbConnection connection;
using (connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Liberty"].ToString()))
{
var result = connection.QueryMultiple("DeleteProduct", new{prodId = Id}, commandType: CommandType.StoredProcedure);
var products = result.Read<Product>().ToList();
return products;
}
}
我如何以错误的方式解决这个问题?
答案 0 :(得分:1)
您可以指定DELETE操作的返回类型是一个数组,因为这是您的Web API控制器返回的内容:
function productResource($resource) {
return $resource("/api/products/:id", { }, {
'delete': {
method: 'DELETE',
isArray: true
}
});
}