12

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:

  • Prevent certain ExampleController from being registered
  • Remove ExampleController and all it's related actions from IServiceCollection after being registered
3
  • If im using autofac lib to register my services like this if (!DataSettingsManager.DatabaseIsInstalled) { if (config.UseFastInstallationService) builder.RegisterType<SqlFileInstallationService>().As<IInstallationService>().InstancePerLifetimeScope(); else builder.RegisterType<CodeFirstInstallationService>().As<IInstallationService>().InstancePerLifetimeScope(); } so i can able to avoid as per my condition so try with autofac to register controller. Commented Jan 7, 2020 at 7:13
  • I think you can find your answer in the link below: Related stackoverflow post Commented Jan 7, 2020 at 7:30
  • 1
    There is also an option to implement custom IApplicationFeatureProvider like in the answer: stackoverflow.com/questions/36680933/… You can implement separate IApplicationFeatureProvider<ControllerFeature> that will filter out specific controller types. You can find simple example in this gist: gist.github.com/Umqra/89b6f26c539a72bdf8c479d5438000dc Commented Jan 7, 2020 at 8:28

1 Answer 1

13

As pointed out in comments, implement feature filter and register it in 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

Sign up to request clarification or add additional context in comments.

4 Comments

Does not work for me... Endpoints are still accessible... (Debugged the IsController method which returns false on the ExampleController)
I think the part of removing the existing ControllerFeatureProvider is missing see: stackoverflow.com/a/50382268/199252
@Roel You might probably want to remove only existing ControllerFeatureProviders, not all FeatureProviders.
On default you only have the existing one and this is the place to add your own custom ones.

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.