1

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?

2 Answers2

1

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.

crgolden
  • 4,332
  • 1
  • 22
  • 40
0

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})"

Rilcon42
  • 9,584
  • 18
  • 83
  • 167