We have ASP.NET Core solution with standard Microsoft.Extensions.DependencyInjection
and need to register certain control depending on configuration setting.
Some Example ApiController
that inherits from ControllerBase
and all their related actions should only be registered if certain bool
is true.
Is this possible? I looked at services.AddMvc()
but I didn't see any option that would easily allow me to either:
ExampleController
from being registeredExampleController
and all it's related actions from IServiceCollection
after being registeredAs pointed out in comments, implement feature filter and register it on your services config:
public class MyFeatureProvider: ControllerFeatureProvider
{
private readonly bool condition;
public MyFeatureProvider(bool condition)
{
this.condition = condition;
}
protected override bool IsController(TypeInfo typeInfo) {
if (condition && typeInfo.Name == "ExampleController") {
return false;
}
return base.IsController(typeInfo);
}
}
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().ConfigureApplicationPartManager(mgr => {
mgr.FeatureProviders.Clear()
mgr.FeatureProviders.Add(new MyFeatureProvider(true));
});
}
}
I'll link the source code in case you'd like to check out stock standard implementation and see how it works for reference
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.