Closed
Description
Synopsis
When versioned controllers that use attribute routing with the same name, but in different namespaces, an InvalidCastException is thrown.
For example:
namespace Example.V1
{
[ApiVersion( "1.0" )]
[Route( "api/helloworld" )]
public class HelloWorldController : ApiController { /* omitted */ }
}
namespace Example.V2
{
[ApiVersion( "2.0" )]
[Route( "api/helloworld" )]
public class HelloWorldController : ApiController { /* omitted */ }
}
namespace Example.V3
{
[ApiVersion( "3.0" )]
[Route( "api/helloworld" )]
public class HelloWorldController : ApiController { /* omitted */ }
}
Analysis
This behavior occurs because although the correct controller type is selected, the action binding is paired to a method from the incorrect candidate controller. For example, api/helloworld?api-version=3.0
selects Example.V3.HelloWorldController
, but binds the action to Example.V1.HelloWorldController.Get
. This behavior only appears to manifest for attribute-routed controllers and only if the controllers also have the same name.