0

I've got two methods in a controller. One accepting a parameter, the other one not.

[Produces("application/json")]
[Route("api/[controller]")]
public class ClientController : Controller
{
    [HttpGet("[action]/{id}")]
    public ObjectResult GetChildNodeObjects(string id)
    {
        //does stuff
    }

    [HttpGet("[action]")]
    public ObjectResult GetChildNodeObjects()
    {
        //does other stuff
    }
}

Now the problem is the first one, the one accepting a parameter. When I hit it with http://localhost:xxxx/api/project/GetChildNodeObjects/231a it will pick up the parameter just fine. But since I get the URL like this: http://localhost:xxxx/api/project/GetChildNodeObjects/?id=231a it goes directly into the other controller method - the one without a parameter. What am I doing wrong for the parameter not to be caught in the second case?

Tetsuya Yamamoto
  • 24,297
  • 8
  • 39
  • 61
kalbsschnitzel
  • 817
  • 1
  • 8
  • 29
  • You need attribute routing - [this post](https://stackoverflow.com/questions/16944947/mvc-attribute-routing-not-working/24409709#24409709) – David McEleney Sep 13 '18 at 09:54

2 Answers2

1

You've included a slash. This slash means that the parameterless action kicks in. So simply replace the URL:

http://localhost:xxxx/api/project/GetChildNodeObjects/?id=231a 

With

http://localhost:xxxx/api/project/GetChildNodeObjects?id=231a 
Luca Kiebel
  • 9,790
  • 7
  • 29
  • 44
csharpforevermore
  • 1,472
  • 15
  • 27
0

You should define in url mappings something like below

routes.MapRoute(
    "myrouting",
    "mycontroller/myaction/",
    new { }
    );
asdf_enel_hak
  • 7,474
  • 5
  • 42
  • 84