kalbsschnitzel
kalbsschnitzel

Reputation: 819

URL with QueryString not hitting MVC Controller method

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?

Upvotes: 0

Views: 562

Answers (2)

asdf_enel_hak
asdf_enel_hak

Reputation: 7640

You should define in url mappings something like below

routes.MapRoute(
    "myrouting",
    "mycontroller/myaction/",
    new { }
    );

Upvotes: 0

csharpforevermore
csharpforevermore

Reputation: 1524

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 

Upvotes: 1

Related Questions