In my web api, I have created this controller:
public class DistributionGroupController : ApiController
{
[HttpGet]
public ServiceResult Index(string id)
{
if (id == null)
return null;
else
return new ServiceResult();
}
}
In addition, this is my route config. I am specifying my default action for my distribution groups route to be "Index":
routes.MapHttpRoute(
"Api action",
"Api/{controller}/{action}"
);
routes.MapHttpRoute(
"Api get",
"Api/{controller}"
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
/*This is the route in question*/
routes.MapHttpRoute(
"DistGroupRoute",
"api/distributiongroup/{id}/{action}",
new { controller = "DistributionGroup", action = "Index" }
);
And in my view, I am using this script to (try to) hit my controller:
$.ajax({
url: "api/distributiongroup/4567bn57n5754",
cache: false,
success: function (response) {
alert('success');
}
});
But my ajax call recieves a 404 Not Found
error. However, if I append index
to my url from my ajax call, my controller is hit. So, in essence, this does not work:
api/distributiongroup/4567bn57n5754
But this does work:
api/distributiongroup/4567bn57n5754/index
It's my understanding that my default action should get hit if I don't specify my action in my url. What might I be missing here? And, more importantly, how can I make my Index
controller get hit when I use a url such as this:
api/distributiongroup/4567bn57n5754
(without specifying the Index
action?