Sayan Pal
Sayan Pal

Reputation: 4956

Inheriting Route attribute messes up already existing actions in Web API

I am trying to inherit Route attributes from a base Controller exactly according to this. Though it seems to work correctly, but it messes up the previously working actions.

Below are a minimal example of my base and child controllers.

[RoutePrefix("api/{controller}")]
public class MyController<TEntity, TDto>: ApiController{
    [HttpGet]
    public IEnumerable<TDto> All(){
        ...
    }

    [HttpGet, Route("lookup")]
    public virtual IEnumerable<TDto> LookupData(){
        ...
    }
}

[RoutePrefix("api/entity")]
public class EntityController : MyController<Entity, DTO>
{        
}

After implementing the route attribute inheritance, the api/entity/lookup action works but in case of api/entity (for All), ActionSelector returns 2 actions, both All, and LookupData, thus causing error.

I am not sure why it is selecting an action with Route attribute even in case of a regular route. What I should do differently? Or is there any robust way to write a ActionSelector for this problem?

Upvotes: 0

Views: 39

Answers (1)

Developer
Developer

Reputation: 6450

Try adding empty [Route] to All method:

[HttpGet]
[Route]
public IEnumerable<TDto> All(){
    ...
}

Upvotes: 0

Related Questions