I am trying to use an abstract base class with generics on an ASP Net Core controller. While the code builds, the endpoints return 404 and do not appear in the swagger docs.
When I remove the generic base class, I am able to see the controller in the docs, but I would then have to copy and paste multiple versions of the controller, which I am trying to avoid.
Any suggestions on what I am missing?
THIS CONTROLLER WORKS
[Route("/testconcrete/employees")]
public class TestController : ControllerBase
{
private readonly IUserService<TechDevsEmployee> _userService;
public TestController(IUserService<TechDevsEmployee> userService)
{
_userService = userService;
}
[HttpGet("{userId}")]
public async Task<ActionResult<User>> GetUserById([FromRoute] Guid userId)
{
var result = await _userService.FindById(userId);
return new OkObjectResult(result);
}
}
THIS CONTROLLER DOES NOT WORK (404)
[Route("/testgeneric/employees")]
public class TechDevsEmployeeController<TechDevsEmployee> : UserController<TechDevsEmployee> where TechDevsEmployee : IUser, IClientEntity
{
public TechDevsEmployeeController(IUserService<TechDevsEmployee> userService) : base(userService)
{
}
}
BASE CLASS
public abstract class UserController<T> : ControllerBase where T : IUser, IClientEntity
{
private readonly IUserService<T> _userService;
public UserController(IUserService<T> userService)
{
_userService = userService;
}
[HttpGet("{userId}")]
public async Task<ActionResult<User>> GetUserById([FromRoute] Guid userId)
{
var result = await _userService.FindById(userId);
return new OkObjectResult(result);
}
}