Reputation: 3897
I have created windows forms application in .netcore 3.1 , that host two WEB Api services, using WebHostBuilder
from the framework dependence of AspNetCore 3.1. (one on port 5000, one on 5001).
I have also created two ApiControllers, with annotations:
[Route("api/[controller]")]
[ApiController]
And the second one:
[Route("api2/[controller]")]
[ApiController]
In both Startup classes, I am setting:
services.AddControllers();
and later:
endpoints.MapControllers()
And it works like a charm.
Now I want to the first controller to be accessible only to port 5000, and the second only to 5001. And the runtime is adding both of them, to the both WEB Api.
So for example:
First Api (at port 5000)
...:5000/api/ - works!
...:5000/api2/ - 404!
Second Api (at port 5001)
...:5001/api/ - 404!
...:5001/api2/ - works!
Is there a way to register specific controller to specific WEB Api, instead of adding everything that is inside your project?
For example, add only controllers, that start with route "api/" or any in specific namespace, or ...any way whatsoever...
Upvotes: 4
Views: 2769
Reputation: 3897
So I have spend the weekend going through every single answer that is even remotely connected to my case.
So basically what you have to do is ConfigureApplicationPartManager like that:
services.AddControllers().ConfigureApplicationPartManager( manager = >...
And then you can add/remove parts. (if you want to remove assembly for example)
And override the IsController
method of ControllerFeatureProvider.
(So you basically tell what is controller and what not.)
This way you can host two separate WEB Api, each with its own set of controllers. I decided to make them in two separate namespaces, in two separate folders, and manage them like that in my project.
From the millions of words I have read, I found those two answers extremely useful: https://stackoverflow.com/a/48787571/6643940
https://stackoverflow.com/a/37647605/6643940
I hope this helps someone in the future.
Upvotes: 4