无法解析控制器
以前我没有为我的通用存储库使用接口。当我从通用存储库中提取接口时,我添加了两个构造函数:无参数构造函数和参数化构造函数,我收到以下错误:
{"Resolution of the dependency failed, type = \"NascoBenefitBuilder.Controllers.ODSController\", name = \"(none)\".
Exception occurred while: while resolving.
Exception is: InvalidOperationException - The current type, ControllerLib.Models.Generic.IGenericRepository, is an interface and cannot be constructed. Are you missing a type mapping?
-----------------------------------------------
At the time of the exception, the container was:
Resolving NascoBenefitBuilder.Controllers.ODSController,(none)
Resolving parameter \"repo\" of constructor NascoBenefitBuilder.Controllers.ODSController(ControllerLib.Models.Generic.IGenericRepository repo)
Resolving ControllerLib.Models.Generic.IGenericRepository,(none)"}
开头的我的控制器:
public class ODSController : ControllerBase
{
IGenericRepository _generic = new GenericRepository();
}
提取接口并在控制器中使用它后:
public class ODSController : ControllerBase
{
IGenericRepository _generic;
public ODSController() : this(new GenericRepository())
{
}
public ODSController(IGenericRepository repo)
{
_generic = repo;
}
}
当我使用参数化构造函数时正在抛出上面提到的错误。
谁能帮助我克服这个问题?
Previously I was not using an interface for my generic repository. When I extracted the interface from my generic repository, I added two constructors: a parameterless and a parameterized constructors I am getting the following error:
{"Resolution of the dependency failed, type = \"NascoBenefitBuilder.Controllers.ODSController\", name = \"(none)\".
Exception occurred while: while resolving.
Exception is: InvalidOperationException - The current type, ControllerLib.Models.Generic.IGenericRepository, is an interface and cannot be constructed. Are you missing a type mapping?
-----------------------------------------------
At the time of the exception, the container was:
Resolving NascoBenefitBuilder.Controllers.ODSController,(none)
Resolving parameter \"repo\" of constructor NascoBenefitBuilder.Controllers.ODSController(ControllerLib.Models.Generic.IGenericRepository repo)
Resolving ControllerLib.Models.Generic.IGenericRepository,(none)"}
My Controller at the beginning:
public class ODSController : ControllerBase
{
IGenericRepository _generic = new GenericRepository();
}
After extracting the interface and use it in controller:
public class ODSController : ControllerBase
{
IGenericRepository _generic;
public ODSController() : this(new GenericRepository())
{
}
public ODSController(IGenericRepository repo)
{
_generic = repo;
}
}
When I use parameterized constructor it is throwing error mentioned above.
Can anyone help me to overcome this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不再需要默认构造函数:
然后确保您已正确配置 Unity 容器:
并且您在
Application_Start
中使用 Unity 控制器工厂:You no longer need the default constructor:
And then make sure you've properly configured your Unity container:
And that you are using the Unity controller factory in
Application_Start
: