I am using ASP.NET Core 3.1 for my web API. I have multiple controllers all using attribute-based routing and all is well.
We want to be able to toggle one or more controllers with feature flags in the app configuration. Ideally if a flag is not set then the corresponding controller should cease to exist in the eyes of the API. I am trying to come up with the best (or any) way to do this.
There appears to be no built-in way to configure which controllers are scanned when using attribute routing, and no way to modify the collection of controllers or endpoints that the routing finds. Here is the Startup.cs snippet in question:
public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory)
{
app.UseRouting();
app.UseEndpoints(e =>
{
if (!this.FeatureEnabled)
{
// DO SOMETHING?
}
e.MapControllers();
});
}
I realize I can probably switch to the more manual hand-written routing and specify every controller, action, and parameter in the Startup class, but I would sooner abandon this feature flag requirement than go down that messy path.
Is there any way to select which controllers are used in the API when using attribute-based routing?