简体   繁体   中英

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?

Try adding empty [Route] to All method:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM