2

Unfortunately, I can't find a solution that fits my requirements, so I decided to create a new question. I know how to create an Controller in asp and configure its route, roles and policy using DataAnnotations like the following example:

[ApiController]
[Route("api/[Controller]")]
[Route("/delete/{id}")]
[Authorize(Roles = "MyRole", Policy = "MyPolicy")]
public class ConcreteController : ControllerBase
{
    //...
}

But I'm searching for a way to register & configure controllers within a single function. Something like the sample bellow.

public void RegisterControllers(IServiceCollection services)
{
    services.AddController(typeof(GenericController<MyModel>), new ControllerConfig{
    Route="api/custom",
    Roles="MyRole",
    Policy="MyPolicy"});
}

I found some posts like the following, but I don't want to solve this problem by using class-inheritance Create a controller programmatically

Vy Do
  • 46,709
  • 59
  • 215
  • 313
  • 1
    I think you need to look into ActionFilterAttribute's. You can decorate a whole class or even a function. – Ross Bush Jun 06 '21 at 02:52
  • `ConcreteController` isn't specified in your second piece of code. Is that intentional? – Neville Nazerane Jun 06 '21 at 07:17
  • Lets say i have a crud controller for adresses, articles and contacts. than i would add all controllers within a function like the RegisterControllers example. My plan is to create modules like you would know from Prism (WPF) and each module has a "Register" function where all services and controllers are added to the IServiceCollection. And it should be possible to replace for example the ArticleModel by an abstracted class within a project. Flow would be : 1. ConfigureModule 2. RegisterModule. – NX ENGINEERING Jun 06 '21 at 19:10

0 Answers0