Ian Vink
Ian Vink

Reputation: 68750

WebApi routing not passing value

I am trying to pass a value to a controller / action in Web Api but it's not finding it.

My Route Mapping:

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{action}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

My ApiController:

    [HttpGet]
    public string MyThing()
    {
        return "thing";
    }

    [HttpGet]
    public string MyStuff(int myid)
    {
        return "something " + myid;
    }

My REST call via RestSharp:

var request = new RestRequest { Resource = "api/values/MyStuff/555", Method = Method.GET };

If I call MyThing() it works though. It seems that the problem is in passing the id value.

Upvotes: 0

Views: 162

Answers (2)

Kiran
Kiran

Reputation: 57949

Modify the parameter name from "myid" to "id"

[HttpGet]
public string MyStuff(int **id**)

Upvotes: 6

Ian Vink
Ian Vink

Reputation: 68750

Solved.

I found I had to add the parameter as an Query String, not a /path value.

api/values/MyStuff?myid=555

instead of

api/values/MyStuff/555

Upvotes: 0

Related Questions