I am creating a restful api with ASP .NET Web Api and I am having trouble with setting up the routing.
Question 1: I am trying to set up a custom delete route. If I name my the action on my controller "DeleteTargetFromApplication" it works perfectly but if I name the action "RemoveTargetFromApplication" it gives me a "The requested resource does not support http method 'DELETE'" error. I change nothing other than the name. The route I use is set up like this:
config.Routes.MapHttpRoute(
name: "PesticideRemoveTargetFromApplication",
routeTemplate: "Pesticide/Applications/{id}/Targets/{targetId}",
defaults: new { controller = "Applications", action = "DeleteTargetFromApplication" },
constraints: new { httpMethod = new HttpMethodConstraint(HttpMethod.Delete) }
)
Is there something that I don't get about how Web Api matches routes?
Question 2: I have all default routes for Get, Post, Put, Delete, Patch. Above the defaults I create any special routes above where the defaults are setup. For some reason, when I add:
config.Routes.MapHttpRoute(
name: "PesticideGetTargetsForApplication",
routeTemplate: "Pesticide/Applications/{id}/Targets",
defaults: new { controller = "Applications", action = "GetTargetsForApplication" },
constraints: new { httpMethod = new HttpMethodConstraint(HttpMethod.Get) }
);
above the defaults, it is saying I have two actions that match, the "GetTargetsForApplication" action and the the default "Get" action I put on all controllers. Why is it seeing both of these actions even if I am specifically telling it which action to use. The route templates are clearly different being "Applications/:id/Targets" and "Applications" respectively. Why are these getting mixed up?