Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
commonsensesoftware opened this issue Nov 4, 2016 · 0 comments
Assignees
Labels

Comments

@commonsensesoftware
Copy link
Collaborator

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.

@commonsensesoftware commonsensesoftware self-assigned this Nov 4, 2016
commonsensesoftware pushed a commit that referenced this issue Nov 6, 2016
…on binding does not correspond to the matched controller. Fixes #44 and resolves #43.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant