Skip to content

InvalidCastException When Attribute-Routed Controllers Have the Same Name #44

Closed
@commonsensesoftware

Description

@commonsensesoftware

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.

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions