c# - Web API 2 Attribute Routing Clash -


i'm having issues getting head around how route selection works. have 2 route attributes set clashing each other. are

[route("{apikey}/parent/{parentid}/children/childrendataformat")] [route("{apikey}/parent/{parentid}/{dataselectiontypea}/{dataselectiontypeb}")] 

the first route's last 2 parts hard coded , never change. second route bind method parameters.

if remove second route first route works fine otherwise 404. presume route matching seeing guid followed "parent" , ignoring fact "children" , "childrendataformat" should present , instead seeing 3 things follow route 2 match.

if correct assumption , there obvious fix make work?

oli

since both routes attribute routes, have no implicit order since both of them have same number of path segments both match leading ambiguity.

the solution differentiate between them, did add constraints 1 of routes match, solution use order first more specific route (the 1 ending /children/childrendataformat).

here simplistic example shows order , how route values being captures

public class valuescontroller : apicontroller {     [route("api/values/myname", order = 1)]     [route("api/values/{name}", order = 2)]     public string get()     {         object nameobj;         request.getroutedata().values.trygetvalue("name", out nameobj);          if (nameobj != null)         {             // came second route             return "route {name} , name was: " + (string) nameobj;         }         else         {             return "route myname no name value available";         }     } } 

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -