0

我正在使用 MVC 4 中可用的两种类型的控制器:System.Web.Mvc.Controller 和 System.Web.Http.ApiController。我需要能够使用 Unity 将依赖项注入到控制器中。

对于我使用 Unity.WebApi 的 ApiControllers: GlobalConfiguration.Configuration.DependencyResolver = new Unity.WebApi.UnityDependencyResolver(_container);

对于我尝试使用使用 Nuget 添加的 Unity.Mvc3 的控制器:DependencyResolver.SetResolver(new Unity.Mvc3.UnityDependencyResolver(_container));

但是,我收到运行时错误:无法加载文件或程序集“System.Web.Mvc,Version=3.0.0.0,Culture=neutral,PublicKeyToken=31bf3856ad364e35”或其依赖项之一。该系统找不到指定的文件。

有谁知道适用于这种情况的解决方案?谢谢

4

1 回答 1

0

在 web.config 中添加绑定重定向:

<runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
      </dependentAssembly>      
    </assemblyBinding>
  </runtime>
于 2012-08-20T15:23:08.153 回答