Reputation: 51
I have the following situation
[Route("api/[controller]")]
[ApiController]
public class FooController: ControllerBase
{
[HttpGet("{id}", Name = "GetFoo")]
public ActionResult<FooBindModel> Get([FromRoute]Guid id)
{
// ...
}
}
[Route("api/[controller]")]
[ApiController]
public class Foo2Controller: ControllerBase
{
[HttpPost("/api/Foo2/Create")]
public ActionResult<GetFooBindModel> Create([FromBody]PostFooBindModel postBindModel)
{
//...
return CreatedAtRoute("GetFoo", new { id = getBindModel.Id }, getBindModel);
}
}
PS: getBindModel
is an instance of type GetFooBindModel
. And I am getting
InvalidOperationException: No route matches the supplied values.
I also tried changing the line
return CreatedAtRoute("GetFoo", new { id = getBindModel.Id }, getBindModel);
to
return CreatedAtRoute("api/Foo/GetFoo", new { id = getBindModel.Id }, getBindModel);
but still the same error.
Upvotes: 5
Views: 3881
Reputation: 323
The following code works for me:
return CreatedAtRoute(
"GetFoo",
new { controller = "Foo", id = getBindModel.Id},
getBindModel);
Upvotes: 0
Reputation: 713
Match the name of your action method (Get) in the FooController with the route name on HttpGet Attribute. You can use the nameof keyword in c#:
[HttpGet("{id}", Name = nameof(Get))]
public ActionResult<FooBindModel> Get([FromRoute]Guid id)
{
...
}
and also Instead of hard coding route name use nameof again:
return CreatedAtRoute(nameof(FooController.Get), new { id = getBindModel.Id }, getBindModel);
and Try again;
Upvotes: 7