4

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?

Nkosi
  • 235,767
  • 35
  • 427
  • 472
Question3r
  • 2,166
  • 19
  • 100
  • 200

1 Answers1

2

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

Nkosi
  • 235,767
  • 35
  • 427
  • 472
  • thanks for your reply. That didn't fix the problem, even when using hardcoded strings like `CreatedAtAction("GetProductByIdAsync", "ProductsController", new { id = 1 }, product);` Rider says it's not able to resolve the method and controller name. – Question3r Jun 07 '20 at 12:04
  • Like so https://cdn.discordapp.com/attachments/165027329981153280/719160075184242758/unknown.png or so https://cdn.discordapp.com/attachments/165027329981153280/719160496791748619/unknown.png – Question3r Jun 07 '20 at 12:07
  • @Question3r those links do not work for me. I am getting an access denied message – Nkosi Jun 07 '20 at 12:09
  • @Question3r based on that last image, `controllerName` should be `Products` (without the `Controller` suffix) – Nkosi Jun 07 '20 at 12:17
  • Ok so this seems to work `return CreatedAtAction(nameof(ProductsController.GetProductByIdAsync), "Products", new { id = 1 }, product);` Is there any way I don't have to hardcode "Products"? Like removing the suffix via `nameof(ProductsController).RemoveSuffix()` – Question3r Jun 07 '20 at 12:34
  • Ok it seems this works too `nameof(Products)`. I don't know why, but the build works ... – Question3r Jun 07 '20 at 12:36
  • @Question3r That could be just luck. Do you have a `Products` class? – Nkosi Jun 07 '20 at 12:37
  • no `Products` is the namespace. But when I hover over it it knows the Controller `(class) API.Products.ProductsController` and when hitting F12 to go to that class it jumps to the controller – Question3r Jun 07 '20 at 12:39
  • @Question3r well then that is new to me. But I guess it is probably some feature of Rider. That does not happen in VS. – Nkosi Jun 07 '20 at 12:40
  • hmm .. tested it with multiple endpoints relying on the same problem and it works. And when firing the request in Postman I get a 201. When analyzing the headers the `Location` field has a correct URL route – Question3r Jun 07 '20 at 12:41
  • i had this problem this morning. Typing the parameter name `actionName:` followed by `""` then gave me a suggested list of actions. In my case, the `Async` on the end of the names was being dropped – nathanjw Mar 19 '21 at 15:13