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.
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;
The following code works for me:
return CreatedAtRoute(
"GetFoo",
new { controller = "Foo", id = getBindModel.Id},
getBindModel);
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.