Within my project I have 2 versions of an API. From this Post I understand that a custom control selector needs writing so it get get the different versions (as webapi looks for controller name match and ignores the namespaces they are in).
This all works OK and I can make the calls to the different versions.
However, I also utilise the new data attribute routing from web api 2 and when using this the customer control selector does not handle the routing correctly.
The routing data attributes I am using are as follows.
(The V01 differs between the versions so it can be V02)
[Route("api/v01/enumeration/{type}/{subtype}")]
This can contain some additional query string params at the end as well
[Route("api/V01/user/readbyreference")]
this takes a query string of ?id=EMAIL|email.domain.com
The code for the customer control selector can be found here
I can see the issue is with GetRouteVariable
to get the needed namespace part and controller, but I was wondering if anyone else has had to do something like this and if they have any way around it.
I will be looking into so if I find something I will update on here but if you have anything please let me know.
Thanks
Mark
After a bit of digging I have found out that attribute routing goes via a different path.
So to handle attribute routing in the GetRouteVariable
you need to grab the MS_SubRoutes
values and then perform the needed action on the result to get the namespace and controller.
The below needs tidying up but it at least gives you the idea of what is done to process data attribute routing in your custom control selector
var subroutes = (IEnumerable<IHttpRouteData>)routeData.Values["MS_SubRoutes"];
var routeBreakDown= subroutes.First().Route.RouteTemplate.Split('/');
if (name == "namespace")
{
return (T)(object)routeBreakDown[1]; //namespace
}
else if (name == "controller")
{
return (T)(object)routeBreakDown[2]; //controller
}
Cheers Mark