Search code examples
c#asp.net-corerider

Rider can't resolve nameof(OtherClass.Method)


Steps to reproduce:

  • Create a new .NET Core Web API project
  • 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

enter image description here

This is annoying because the IDE comes up with it in the project explorer too

enter image description here

Although the code works fine. So is my code bad and I should improve it? Is it a bug?


Solution

  • 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