voglio ottenere qualcosa vicino all'azione RateProduct descritto: http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/odata-actions.Net WebAPI OData azioni che restituiscono un Queryable
In questo tutorial è definito come:
[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>();
Tuttavia, ho il utilizzare il caso di un'entità di posizione che sia abbastanza intelligente da restituire altre posizioni entro un determinato raggio attorno ad essa. Dovrebbe essere più o meno in questo modo:
[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>>();
mi piacerebbe arrivare a questo lavoro e attualmente non funziona quando il tipo di ritorno è un IQueryable<Location>
. Se il tipo di ritorno è un primitivo come un int, allora funziona, altrimenti si dà il seguente errore quando creo un post nel violinista (il post è qualcosa come http://localhost:2663/odata/Locations(2112)/GetLocationsWithinRadius
e la richiesta Corpo è {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)"
}
}
}
}
È possibile fare ciò che sto cercando di realizzare? E se lo è, oso chiederti se il reso IQueryable<Location>
diventa componibile con i parametri odata ... (sarebbe bello)?
Grazie
Vedo che il tipo di set di entità è 'Prodotto'. Dovrebbe essere "Posizione" ?. Inoltre ottieni ulteriori dettagli sull'errore ... se sì, potresti condividere ... sarebbe utile. Puoi ottenere maggiori dettagli facendo 'config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always'. –
Sì, l'ho appena risolto, il mio male. Ho scritto il codice a mano in SO invece di copiare e incollare. Sto anche modificando il post per mostrare l'errore completo. – t316