c# - Unable to POST to Web API endpoint using attribute routing -


i have web api project uses default route of "api/{controller}/{id}" "id" optional. in cases sufficient in cases need attribute routing. in specific case, have "api/customers/{customerid}/orders/{orderid}" customer id required , orderid optional. code required order id needed way pull orders particular customer used attribute route on methods allow this.

i able perform operations without problem, when try post operation, 500 error. what's odd though object gets created exception gets thrown must coming after database insert created cannot confirm since debugger doesn't work. api in separate project ui , whatever reason cannot debugger work in api project breakpoints have set don't work.

the last line of code in post method this:

return createdatroute("defaultapi", new { id = order.id }, order); 

the first argument of method route name , 1 listed above default route specified in webapiconfig.cs. particular route different default:

[route("api/customers/{customerid:int}/orders")] 

could problem? since route in question uses 2 arguments, assume i'd need specify them in routevalues (second) argument createdatroute method.

what need make work? suspect may have problems performing put , delete operations well, need create object before can modify or delete it.

okay, solved myself. need set name attribute on route , use first argument createdatroutemethod. needed specify 2 route values corresponding 2 method arguments. able perform put , delete operations without problems.

[route("api/customers/{customerid:int}/orders", name = "test")]  return createdatroute("test", new { customerid = customer.customerid, orderid = order.id }, order); 

Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -