I have ODataController with a Post method in it which should return a URL to a newly created OData resource, something like the following:
public class TasksController: ODataController
{
[HttpPost]
public IActionResult Post([FromBody] Request request)
{
...
return CreatedAtRoute("GetTask", new Dictionary<string, object>{{"id", id}}, new object());
}
[ODataRoute(RouteName = "GetTask")]
public IActionResult Get(int key)
{
...
}
}
In my case I'm getting "InvalidOperationException: No route matches the supplied values" when returning CreatedAtRoute. I can fix the issue by changing code to:
return Created($"{baseUri}/odata/Task({id})", new object());
Is there any way to use CreatedAtRoute instead and making it return correct OData path?
I had this problem also. I was able to get it working by adding "odataPath" to the routeValues
:
return CreatedAtAction(nameof(Get), new { id, odataPath = $"{baseUri}/odata/Task({id})" }, new object());
UPDATE:
I did find an alternate/better approach. When inheriting from ODataController
, you have access to two additional result types: CreatedODataResult<TEntity>
and UpdatedODataResult<TEntity>
. So this works:
return Created(new object());
That returns a 201 with the OData-specific create route in the location header.