Question Dynamically registering controllers in ASP.NET Core?

utf_8x

New member
Joined
Oct 8, 2024
Messages
1
Programming Experience
5-10
I've been googling around trying to wrap my head around this but I can't seem to find the right solution that would actually work. ChapGPT has no idea either.

The gist of it is, I have a bunch of model classes that all implement IModel. I need to, at runtime, go through all available implementations of IModel and register an instance of my GenericController for them.

IModel is an empty interface.

The GenericController looks like this:

GenericController.cs:
using IronERP.Core.Data;
using Microsoft.AspNetCore.Mvc;

namespace IronERP.Web.Controllers;

[ApiController]
[Route("/api/v1/[controller]")]
public class GenericController<T> : ControllerBase where T : IModel
{
    public CrudController(ModelDaoFactory modelDaoFactory)
    {
        _modelDaoFactory = modelDaoFactory;
        _dao = _modelDaoFactory.Create<T>();
    }

    [HttpGet]
    [Route("/")]
    public async Task<IActionResult> GetAll()
    {
        var items = await _dao.GetAll();
        return Ok(items);
    }
}

I've tried various obscure incantations of ApplicationPartManager but no matter what I do, I can't get the controllers to register. Has anyone done this before? Any idea what to do here?
 
Can you show us the incantations you have tried?
 
Back
Top Bottom