Is it possible to get OData to do the following? I would like to be able to query a REST call by passing on parameters than may not be the primary key.
Can I call a REST method like --> GetReports(22, 2014)
or Reports(22, 2014)
?
[HttpGet]
[ODataRoute("Reports(Id={Id}, Year={Year})")]
public IHttpActionResult GetReports([FromODataUri]int Id, [FromODataUri]int Year)
{
return Ok(_reportsRepository.GetReports(Id, Year));
}
Here is my latest change.
//Unbound Action OData v3
var action = builder.Action("ListReports");
action.Parameter<int>("key");
action.Parameter<int>("year");
action.ReturnsCollectionFromEntitySet<Report>("Reports");
my method for controller ReportsController
[HttpPost]
[EnableQuery]
public IHttpActionResult ListReports([FromODataUri] int key, ODataActionParameters parameters)
{
if (!ModelState.IsValid)
{
throw new HttpResponseException(HttpStatusCode.BadRequest);
}
int year = (int)parameters["year"];
return Ok(_reportsRepository.GetReports(key, year).Single());
}
I tried calling the url like:
http://localhost:6064/odata/Reports(key=5,year=2014)/ListReports
No HTTP resource was found that matches the request URI 'http://localhost:6064/odata/Reports(key%3D5%2Cyear%3D2014)/ListReports'
.`
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…