Reputation: 11019
I have a an ASP.NET Web API project where I have a single method in my controller as such ..
public IHttpActionResult MyMethod(int param1, int param2, int param3)
{
var theSum = param1 + param2 + param3;
return Ok(theSum);
}
I have the following route in RouteConfig.cs
routes.MapRoute(
name: "MyRoute",
url: "api/{controller}/{action}/{param1}/{param2}/{param3}",
defaults: new { controller = "MyController", action = "MyRoute", param1 = UrlParameter.Optional, param2 = UrlParameter.Optional, param3 = UrlParameter.Optional }
);
When I call the API with the following URL everything works as expected ..
http://localhost/api/mycontroller/mymethod?param1=2¶m2=4¶m3=6
Yet when I try to call the API as follows I get a 404 - The resource cannot be found
error.
http://localhost/api/mycontroller/mymethod/2/4/6/
any idea why? I thought I had the route setup properly and since the parameters are .NET primitives I though I could pass them as param1/param2/param3
Upvotes: 0
Views: 424
Reputation: 1217
Are you using MVC5? If so I'd suggest using the Route() attribute instead the routes collection, for me at least it is easier to manage. I was able to get this working with your sample using that approach.
[Route("api/values/MyMethod/{param1}/{param2}/{param3}")]
[HttpGet]
public IHttpActionResult MyMethod(int param1, int param2, int param3)
{
var theSum = param1 + param2 + param3;
return Ok(theSum);
}
And then calling it via http://localhost/api/values/mymethod/2/4/6 properly returned 12.
Upvotes: 2