3

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

3
  • 1
    I think you need to look into ActionFilterAttribute's. You can decorate a whole class or even a function. Commented Jun 6, 2021 at 2:52
  • ConcreteController isn't specified in your second piece of code. Is that intentional? Commented Jun 6, 2021 at 7: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. Commented Jun 6, 2021 at 19:10

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.