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.
The route your are returning in the Created method: "{baseUri}/odata/Task({id})"
doesn't exist. The simplest fix would be to change your URL to match your controller method.
Change: $"{baseUri}/odata/Task({id})"
to match $"{baseUri}/odata/GetTask({id})"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.