one API controller class for all tables

so I’m making a EFCore API to connect my MAUI app to my SQLServer database.

This is my “tblUser” table’s controller (Select, Create, Update, Delete, all the usual stuff):

UserController.cs:

[Route("api/[controller]")]
[ApiController]
public class tblUserController : ControllerBase
{
    private readonly QDbContext _qDbContext;

    public tblUserController(QDbContext qDbContext) => _qDbContext = qDbContext;

    [HttpGet]
    public ActionResult<IEnumerable<tblUser>> Get()
    {
        return _qDbContext.tblUsers;
    }

    [HttpGet("{id}")]
    public async Task<ActionResult<tblUser?>> GetById(int id)
    {
        return await _qDbContext.tblUsers.Where(x => x.ID == id).SingleOrDefaultAsync();
    }

    [HttpPost]
    public async Task<ActionResult> Create(tblUser user)
    {
        await _qDbContext.tblUsers.AddAsync(user);
        await _qDbContext.SaveChangesAsync();

        return CreatedAtAction(nameof(GetById), new { id = user.ID }, user);
    }

    [HttpPut]
    public async Task<ActionResult> Update(tblUser user)
    {
        _qDbContext.tblUsers.Update(user);
        await _qDbContext.SaveChangesAsync();
        return Ok();
    }

    [HttpDelete("{id}")]
    public async Task<ActionResult> Delete(int id)
    {
        var userGetByIdResult = await GetById(id);
        if (userGetByIdResult.Value is null)
            return NotFound();

        _qDbContext.Remove(userGetByIdResult.Value);
        await _qDbContext.SaveChangesAsync();
        return Ok();
    }
}

I really didn’t want to repeat the same thing for each table, so I made a parent class that gets the type as “TEntity” and does the same stuff:

DbController.cs

[Route("api/[controller]")]
[ApiController]
public class DbController<TEntity> : ControllerBase where TEntity : class
{
    private readonly DbContext _context;
    private readonly DbSet<TEntity> _dbSet;


    public DbController(DbContext context)
    {
        _context = context;
        _dbSet = _context.Set<TEntity>();
    }

    [HttpGet]
    public async Task<ActionResult<IEnumerable<TEntity>>> Get()
    {
        return await _dbSet.ToListAsync();
    }

    [HttpGet("{id}")]
    public async Task<ActionResult<TEntity>> GetById(int id)
    {
        TEntity? entity = await _dbSet.FindAsync(id);
        return entity == null ? (ActionResult<TEntity>)NotFound() : (ActionResult<TEntity>)entity;
    }

    [HttpPost]
    public async Task<ActionResult> Create(TEntity entity)
    {
        _ = await _dbSet.AddAsync(entity);
        _ = await _context.SaveChangesAsync();

        return CreatedAtAction(nameof(GetById), new { id = entity.GetType().GetProperty("ID")?.GetValue(entity) }, entity);
    }

    [HttpPut("{id}")]
    public async Task<ActionResult> Update(int id, TEntity entity)
    {
        TEntity? existingEntity = await _dbSet.FindAsync(id);
        if (existingEntity == null)
        {
            return NotFound();
        }

        _context.Entry(existingEntity).CurrentValues.SetValues(entity);
        _ = await _context.SaveChangesAsync();
        return Ok();
    }

    [HttpDelete("{id}")]
    public async Task<ActionResult> Delete(int id)
    {
        TEntity? entity = await _dbSet.FindAsync(id);
        if (entity == null)
        {
            return NotFound();
        }

        _ = _dbSet.Remove(entity);
        _ = await _context.SaveChangesAsync();
        return Ok();
    }
}

and inherited my User controller from it:

UserController.cs

[Route("api/[controller]")]
[ApiController]
public class tblUsersController : DbController<tblUser>
{
    public tblUsersController(QDbContext context) : base(context)
    {
    }
}

The problem is that when I use GetItems() I get a 404 error although swagger works fine!

also when I roll back my parent class making, GetItems() works.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật