Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I make an ASP.NET Core controller internal?

ASP.NET (and Core) controllers need to be public.

Problem is I have a controller which depends (in its constructor) on something internal. And that dependency depends on something internal, which depends on something internal, etc. So I need to make the controller internal as well.

But then it won't be discovered by the controller factory.

Is there a way to make an internal controller discoverable?

like image 980
grokky Avatar asked Jan 24 '17 09:01

grokky


People also ask

How do I add a controller in NET Core?

Select the EXPLORER icon and then control-click (right-click) Controllers > New File and name the new file HelloWorldController. cs . In Solution Explorer, right-click Controllers > Add > New File. Select ASP.NET Core and Controller Class.

Can a controller have a constructor?

A visualforce page doesn't require a controller. However, if it uses a controller, the controller needs a constructor. The controller is a class. The constructor tells us how to create an instance of that class, and the VF page uses that instance to know what information to display.

What is controller in ASP.NET Core?

The controller takes the result of the model's processing (if any) and returns either the proper view and its associated view data or the result of the API call. Learn more at Overview of ASP.NET Core MVC and Get started with ASP.NET Core MVC and Visual Studio. The controller is a UI-level abstraction.


2 Answers

If you want to include internal controllers, you can provide your own implementation of the ControllerFeatureProvider class which is responsible for determining whether a type is a controller or not. In the following example I have created an implementation that looks for controllers implementing a custom base class and falling back to the default implementation for all other cases. In this case, the custom controllers will be discoverable despite being internal, while all other controllers will not.

class CustomControllerFeatureProvider : ControllerFeatureProvider
{
    protected override bool IsController(TypeInfo typeInfo)
    {
        var isCustomController = !typeInfo.IsAbstract && typeof(MyCustomControllerBase).IsAssignableFrom(typeInfo);
        return isCustomController || base.IsController(typeInfo);
    }
}

and to register it:

services.AddMvc().ConfigureApplicationPartManager(manager =>
{
    manager.FeatureProviders.Add(new CustomControllerFeatureProvider());
});

You should probably take a look at the implementation of IsController to see how ASP.NET handles edge cases around the types.

like image 183
Morten Christiansen Avatar answered Oct 17 '22 03:10

Morten Christiansen


Sou you have this (it always helps to include a MCVE in your question):

internal class FooDependency
{

}

public class FooController
{
    public FooController(FooDependency dependency)
    {
        // ...
    }
}

And you can't make FooDependency public, but you need FooController to be public?

Then you need to apply a public interface to the internal dependencies:

public interface IFooDependency
{

}

internal class FooDependency : IFooDependency
{

}

public class FooController
{
    public FooController(IFooDependency dependency)
    {
        // ...
    }
}
like image 40
CodeCaster Avatar answered Oct 17 '22 05:10

CodeCaster