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?

1
  • You need attribute routing - this post Commented Sep 13, 2018 at 9:54

2 Answers 2

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 
Sign up to request clarification or add additional context in comments.

1 Comment

have you any custom route maps or are they MVC default ones?
0

You should define in url mappings something like below

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

1 Comment

Where you define your route, you can enable attribute routing with ' routes.MapMvcAttributeRoutes();'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.