Steps to reproduce:
Head over to the generated WeatherForecastController
, update the class to
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
[HttpGet("{id}")]
public async Task<ActionResult<object>> GetById(int id)
{
return Ok(null);
}
}
Create a second controller
[ApiController]
[Route("[controller]")]
public class FooController : ControllerBase
{
[HttpPost]
public async Task<ActionResult<object>> Create()
{
return CreatedAtAction(nameof(WeatherForecastController.GetById), new { id = 1 }, null);
}
}
Build be project, it should be fine
Rider comes up with an error at the Create
route
Cannot resolve action 'GetById'
As you can see here
This is annoying because the IDE comes up with it in the project explorer too
Although the code works fine. So is my code bad and I should improve it? Is it a bug?
The wrong overload of CreatedAtAction
is being used.
It cannot resolve action GetById
because the overload used expects the action to belong to the current controller (ie FooController
).
If the intended action belongs to another controller then the controller name needs to be included.
FooController:
//...
[HttpPost]
public ActionResult<object> Create() {
return CreatedAtAction(
actionName: "GetById",
controllerName: "WeatherForecast", //Note removal of `Controller` suffix
routeValues: new { id = 1 },
value: null);
}
//...
Reference ControllerBase.CreatedAtAction Method