c# - Routing not working as expected -
i creating restful api asp .net web api , having trouble setting routing.
question 1: trying set custom delete route. if name action on controller "deletetargetfromapplication" works if name action "removetargetfromapplication" gives me "the requested resource not support http method 'delete'" error. change nothing other name. route use set 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 don't how web api matches routes?
question 2: have default routes get, post, put, delete, patch. above defaults create special routes above defaults setup. reason, when 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 defaults, saying have 2 actions match, "gettargetsforapplication" action , the default "get" action put on controllers. why seeing both of these actions if telling action use. route templates different being "applications/:id/targets" , "applications" respectively. why these getting mixed up?
on question 1:
the routing engine convention based , determines allowable http verbs name of method (action). thats why deletetargetfromapplication works.
a simple work around add [httpdelete] definition of removetargetfromapplication.
from http://www.asp.net/web-api/overview/web-api-routing-and-actions/routing-in-aspnet-web-api
to find action, web api looks @ http method, , looks action name begins http method name. example, request, web api looks action starts "get...", such "getcontact" or "getallcontacts". convention applies get, post, put, , delete methods. can enable other http methods using attributes on controller. we’ll see example of later.
Comments
Post a Comment