tôi muốn đạt được một cái gì đó gần với hành động được mô tả trong RateProduct: http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/odata-actionsActions Net WebAPI OData mà trả về một queryable
Trong hướng dẫn rằng nó được định nghĩa là:
[HttpPost]
public int RateProduct([FromODataUri] int key, ODataActionParameters parameters)
{
// ...
}
ODataModelBuilder modelBuilder = new ODataConventionModelBuilder();
modelBuilder.EntitySet<Product>("Products");
// New Code
ActionConfiguration rateProduct = modelBuilder.Entity<Product>().Action("RateProduct");
rateProduct.Parameter<int>("Rating");
rateProduct.Returns<int>();
Tuy nhiên, tôi có trường hợp sử dụng của thực thể Vị trí đủ thông minh để trả lại các Vị trí khác trong một bán kính nhất định xung quanh nó. Nó sẽ gần như thế này:
[HttpPost]
public IQueryable<Location> GetLocationsWithinRadius([FromODataUri] int key, ODataActionParameters parameters)
{
// Get the Location instance intended to be the center of the radius by using the key
// Do a radius search around it using (int)parameters["radius"] as the radius
// return the IQueryable<Location> of all location found within that radius
}
ODataModelBuilder modelBuilder = new ODataConventionModelBuilder();
modelBuilder.EntitySet<Location>("Locations");
// New Code
ActionConfiguration getLocations = modelBuilder.Entity<Location>().Action("GetLocationsWithinRadius");
getLocations.Parameter<int>("radius");
getLocations.Returns<IQueryable<Location>>();
Tôi rất muốn làm việc này và hiện tại nó không hoạt động khi kiểu trả về là IQueryable<Location>
. Nếu kiểu trả về là một nguyên thủy như một int, sau đó nó hoạt động, nếu không nó mang lại cho các lỗi sau khi tôi tạo một bài đăng trong cáy (bài này là một cái gì đó giống như http://localhost:2663/odata/Locations(2112)/GetLocationsWithinRadius
và Yêu cầu cơ thể là {radius: 50}
):
{
"odata.error":{
"code":"","message":{
"lang":"en-US","value":"An error has occurred."
},"innererror":{
"message":"The 'ObjectContent`1' type failed to serialize the response body for content type 'application/json; odata=minimalmetadata; streaming=true; charset=utf-8'.","type":"System.InvalidOperationException","stacktrace":"","internalexception":{
"message":"The related entity set could not be found from the OData path. The related entity set is required to serialize the payload.","type":"System.Runtime.Serialization.SerializationException","stacktrace":" at System.Web.Http.OData.Formatter.Serialization.ODataFeedSerializer.WriteObject(Object graph, ODataMessageWriter messageWriter, ODataSerializerContext writeContext)\r\n at System.Web.Http.OData.Formatter.ODataMediaTypeFormatter.<>c__DisplayClassa.<WriteToStreamAsync>b__9()\r\n at System.Threading.Tasks.TaskHelpers.RunSynchronously(Action action, CancellationToken token)"
}
}
}
}
Có thể làm những gì tôi đang cố gắng hoàn thành không? Và nếu nó là, tôi dám hỏi nếu trở về IQueryable<Location>
trở nên composable với paramaters odata ... (đó sẽ là tốt đẹp)?
Cảm ơn
Tôi thấy rằng loại tổ chức của bạn là 'Sản phẩm'. Đây có phải là 'Vị trí' không ?. Ngoài ra bạn có nhận được thêm bất kỳ chi tiết lỗi nào không ... nếu có, bạn có thể chia sẻ ... nó sẽ hữu ích. Bạn có thể lấy thêm chi tiết bằng cách thực hiện 'config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always'. –
Vâng - Tôi vừa sửa lỗi đó - xấu của tôi. Tôi đã viết mã trong SO thay vì sao chép và dán. Tôi cũng đang chỉnh sửa bài đăng để hiển thị lỗi đầy đủ. – t316