Reputation: 15
Hello guys I was wondering if there a way to override an action controller that inherited from a base controller just for add a filter attribute , this filter attribute allows to validate if a user has a certain permission to execute an action controller , here’s my base controller.
public class BaseController<T> : ApiController where T : Entity<int>
{
private readonly IEntityService<T> _service;
public BaseController(IEntityService<T> service )
{
_service = service;
}
public BaseController()
{
}
public IEnumerable<T> GetAllEntities()
{
return _service.GetAll();
}
}
And here's my sub controller that I want to add the filter attribute note the code below is incorrect but I want to do something like this , do you know how to do it ? thanks for your attention..
public class ArbitrosController : BaseController<Arbitro>
{
public ArbitrosController(IArbitroService _ArbitroService):base( _ArbitroService )
{
}
[HasPermission("get")]//filter atribute
public new IEnumerable<Arbitro> GetAllEntities()
{
}
}
Upvotes: 1
Views: 3353
Reputation: 412
Use virtual
keyword in base class and override
in descendant. To access base class from overrided you can use base
keyword:
public class BaseController<T> : ApiController where T : Entity<int>
{
...
public virtual IEnumerable<T> GetAllEntities()
{
return _service.GetAll();
}
}
public class ArbitrosController : BaseController<Arbitro>
{
...
[HasPermission("get")]//filter atribute
public override IEnumerable<Arbitro> GetAllEntities()
{
return base.GetAllEntities();
}
}
Here is a tutorial about inheritance in C#.
Upvotes: 2