小编Yar*_*lav的帖子

确保控制器在Unity中具有无参数的公共构造函数

我在控制器中遇到了这个问题:
尝试创建类型为' * .WebMvc.Controllers.HomeController' 的控制器时发生错误.确保控制器具有无参数的公共构造函数.

找到ApiController的解决方案,但没有找到任何关于普通Controller的信息.

从头开始创建新的MVC 4项目.

HomeController.cs:

public class HomeController : Controller
{
    private IAccountingUow _uow;
    public HomeController(IAccountingUow uow)
    {
        _uow = uow;
    }
Run Code Online (Sandbox Code Playgroud)

UnityDependencyResoler.cs:

public class UnityDependencyResolver : IDependencyResolver
{
    private IUnityContainer _container;
    public UnityDependencyResolver(IUnityContainer container)
    {
        _container = container;
        RegisterTypes();
    }
    public object GetService(Type serviceType)
    {
        try
        {
            return _container.Resolve(serviceType);
        }catch
        {
            return null;
        }
    }

    public IEnumerable<object> GetServices(Type serviceType)
    {
        try
        {
            return _container.ResolveAll(serviceType);
        }catch
        {
            return null;
        }
    }

    private void RegisterTypes()
    {
        _container.RegisterType<IAccountingUow, …
Run Code Online (Sandbox Code Playgroud)

c# dependency-injection unity-container asp.net-mvc-4

7
推荐指数
1
解决办法
2万
查看次数