Reputation: 107
I have the following in my WebApiConfig.cs file:
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "UserLogin",
routeTemplate: "api/{controller}/{UserInitials}/{UserPin}",
defaults: new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "BinQuery",
routeTemplate: "api/{controller}/{UserID}/{UserCode}/{BinCode}",
defaults: new { id = RouteParameter.Optional }
);
//This one below does not work as one above is always taken first!!
config.Routes.MapHttpRoute(
name: "BarcodeQuery",
routeTemplate: "api/{controller}/{UserID}/{UserCode}/{BarCode}",
defaults: new { id = RouteParameter.Optional }
);
I would like to be able specify by the URL called which controller to use, at the moment, because the last entry has three parameters it never get chosen, the middle one does.
For example, I would like the following URLs to call controllers I specify:
http://myserver/api/UserLogin/AS/1234
http://myserver/api/BinQuery/AS/1234/ABC123
http://myserver/api/BarcodeQuery/AS/1234/3424532543
Hope this is somewhat clear what I am trying to achieve.
Upvotes: 1
Views: 1560
Reputation: 247433
They have the same route template so there is a route conflict.
You have to decide on how to uniquely distinguish one from the other.
If those routes belong to a specific controller then use that as the distinguishing factor.
You also want more specific route to come before more general routes. So place the UserLogin
route last since uses {controller}
placeholder in its template
config.Routes.MapHttpRoute(
name: "BinQuery",
routeTemplate: "api/BinQuery/{UserID}/{UserCode}/{BinCode}",
defaults: new { controller = "BinQuery" }
);
config.Routes.MapHttpRoute(
name: "BarcodeQuery",
routeTemplate: "api/BarcodeQuery/{UserID}/{UserCode}/{BarCode}",
defaults: new { controller = "BarcodeQuery" }
);
config.Routes.MapHttpRoute(
name: "UserLogin",
routeTemplate: "api/{controller}/{UserInitials}/{UserPin}",
defaults: new { id = RouteParameter.Optional }
);
Upvotes: 1