c# - Web API Routing conflict when adding PUT method to subroute, with multiple controllers -
i'm not sure how name problem. had difficulty searching particular issue reason. closest matching question found web api routing conflict 2 different controllers?.
like asker in question, have 2 different controllers partially match:
public class jobscontroller : apicontroller { [responsetype(typeof(dtjob[]))] public async task<httpresponsemessage> get() { //... } [route("v1/jobs/group"), responsetype(typeof(dtjob[]))] public async task<httpresponsemessage> getfromgroup() { //... } [httpget, route("v1/jobs/{jobid}/status"), responsetype(typeof(string))] public async task<httpresponsemessage> getstatus(long jobid) { //... } [httpput, route("v1/jobs/{jobid}/status"), responsetype(typeof(void))] public async task<httpresponsemessage> updatestatus(long jobid, string status) { //... } //this method messes other controllers [httpput, route("v1/jobs/{jobid}"), responsetype(typeof(void))] public async task<httpresponsemessage> put(long jobid, dtjob job) { //... } //but works if replace above method default routing pattern: //[responsetype(typeof(void))] //public async task<httpresponsemessage> put(long id, dtjob job) //{ // //... //} }
and controller has similar prefix:
[routeprefix("v1/jobs/materials")] public class materialscontroller : apicontroller { [route, responsetype(typeof(dtmaterial[]))] public async task<httpresponsemessage> get() { //... } }
and routing configs:
public class routeconfig { public static void registerroutes(routecollection routes) { routes.ignoreroute("{resource}.axd/{*pathinfo}"); routes.mapmvcattributeroutes(); } } public static class webapiconfig { public static void register(httpconfiguration config) { //... // web api routes config.maphttpattributeroutes(); //... config.routes.maphttproute( name: "newest", routetemplate: "v1/{controller}/{id}", defaults: new { id = routeparameter.optional } ); } }
what's going on here?
i think found issue again. since {jobid}
not specified in type, potentially string says "materials", creating conflict. resolve this, specify type in parameter ({jobid:long}
):
[httpput, route("v1/jobs/{jobid:long}"), responsetype(typeof(void))] public async task<httpresponsemessage> put(long jobid, dtjob job) { //... }
Comments
Post a Comment