Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 775
Question3r Avatar asked Oct 16 '25 16:10

Question3r


1 Answers

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

like image 135
Nkosi Avatar answered Oct 18 '25 07:10

Nkosi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!