I am using namespace versioning in my web api (4.0) project. I'm using Attribute routing to define custom routes for our actions. This works just fine when a controller only exists in one namespace/version.
But when a controller exists in both namespaces/versions, the attribute route is ignored completely. I am using the solution here to look at what routes are in the routing table. And it only contains one route, for my "single" controller. If I delete/comment out/change the name of the controller in V2, then suddenly the "double" controller's route appears in the routing table as well.
Is there any reason that an attribute route would be ignored because the same class/controller name exists in 2 different namespaces? Here is my code, broken down to the simplest failing example:
namespace API.v1
{
public class SingleController : ApiController
{
[HttpGet]
[Route("api/{version}/Single")]
public string Test()
{
return "Response";
}
}
}
This class/route works just fine. http://localhost:57560/api/v1/Single returns "Response".
namespace API.v1
{
public class DoubleController : ApiController
{
[HttpGet]
[Route("api/{version}/Double")]
public string Test()
{
return "Response";
}
}
}
namespace API.v2
{
public class DoubleController : ApiController
{
[HttpGet]
[Route("api/{version}/Double")]
public string Test()
{
return "Response";
}
}
}
These 2 classes fail; the attribute route is missing from the routing table, and http://localhost:57560/api/v1/Double returns 404, as well as http://localhost:57560/api/v2/Double. If I just change the name of the v2.Double class, then it works just fine.
In this example, I have no default routes set up at all; only the Attribute routing. When using default routes, the versioning and routing work just fine:
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{version}/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
This route works perfectly across multiple versions. But I need it to work with attribute routing.
I have been able to solve the issue using the solution here: http://abhinawblog.blogspot.com/2014/12/web-api-versioning-using.html. The problem appears to be related to the way that attribute routing differs from using the default routes table. It is required that the routes have unique names, and that the version numbers in the routes have custom constraints put on them.
In order to make the namespace versioning possible, you need to override the default behavior of routing. Check this MSDN Link for more details
Also check this sample code to see how it's done