问题描述
ASP.NET(和核心)控制器必须为public
.
ASP.NET (and Core) controllers need to be public
.
问题是我有一个控制器,该控制器(在其构造函数中)依赖于某些internal
.而且这种依赖关系取决于内部的东西,也取决于内部的东西,等等.因此,我还需要将控制器设为internal
.
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.
有没有办法使internal
控制器可被发现?
Is there a way to make an internal
controller discoverable?
推荐答案
这样您就可以了(包括 MCVE 您的问题):
Sou you have this (it always helps to include a MCVE in your question):
internal class FooDependency
{
}
public class FooController
{
public FooController(FooDependency dependency)
{
// ...
}
}
您不能公开FooDependency
,但是您需要公开FooController
吗?
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)
{
// ...
}
}
这篇关于我可以在内部设置ASP.NET Core控制器吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!