C# Automapper在具有MvcExtensions的MVC4应用程序中请求MVC3引用
我使用的是C# Automapper在具有MvcExtensions的MVC4应用程序中请求MVC3引用,c#,asp.net-mvc,asp.net-mvc-4,automapper,mvcextensions,C#,Asp.net Mvc,Asp.net Mvc 4,Automapper,Mvcextensions,我使用的是ASP.NET MVC 4 RC和最新版本的MvcExtensions和MvcExtensions.Autofac 我不知道MVC4和MVC3的工作原理是否不同?下面的代码是我在MVC3应用程序中如何使用它的。我刚刚复制并粘贴到我的MVC4应用程序中 我将Global.asax.cs文件替换为如下所示: public class MvcApplication : AutofacMvcApplication { public MvcApplication() {
ASP.NET MVC 4 RC
和最新版本的MvcExtensions
和MvcExtensions.Autofac
我不知道MVC4和MVC3的工作原理是否不同?下面的代码是我在MVC3应用程序中如何使用它的。我刚刚复制并粘贴到我的MVC4应用程序中
我将Global.asax.cs文件替换为如下所示:
public class MvcApplication : AutofacMvcApplication
{
public MvcApplication()
{
Bootstrapper.BootstrapperTasks
.Include<RegisterAreas>()
.Include<RegisterControllers>()
.Include<RegisterRoutesBootstrapper>()
.Include<AutoMapperBootstrapper>()
.Include<FluentValidationBootstrapper>();
}
protected override void OnStart()
{
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
base.OnStart();
}
}
public class AutoMapperBootstrapper : BootstrapperTask {
public override TaskContinuation Execute()
{
const string mappingNamespace = "MyProject.DomainModel.Mappings";
IEnumerable<Type> mappingTypes = typeof(IEntity).Assembly
.GetTypes()
.Where(
type =>
type.IsPublic &&
type.IsClass &&
!type.IsAbstract &&
!type.IsGenericType &&
type.Namespace == mappingNamespace);
mappingTypes.ForEach(t => Activator.CreateInstance(t));
return TaskContinuation.Continue;
} }
它以蓝色下划线IEnumerable,并显示错误:
The type 'System.Web.Mvc.Controller' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'.
好的,当我编译我的项目时,它正在寻找ASP.NET MVC 3参考:
The type 'System.Web.Mvc.IViewPageActivator' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. There about 10 such errors relating to the AutoMapperBootstrapper.cs file.
我没有考虑这个参考,而是添加了MVC4参考。我原以为这会解决我的问题,但事实并非如此
你知道它为什么要求提供MVC参考吗?我猜
MvcExtensions
和MvcExtensions.Autofac
NuGet包是根据ASP.NET MVC 3编译的,并且对System.Web.MVC V3.0.0有很强的参考价值。它们与ASP.NET MVC 4不兼容。您必须联系这些软件包的作者,以向您提供根据ASP.NET MVC 4编译的更新版本。请记住,ASP.NET MVC 4尚未启用RTM,因此不要期望所有NuGet软件包立即获得更新版本。问题在于以下几行:
typeof(IEntity).Assembly
.GetTypes()
请看Jon Skeet在哪里解释为什么会发生这种情况以及如何处理
另外,您还可以查看Phil Haack的文章,添加绑定重定向应该会对您有所帮助。至少添加以下重定向:
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
一个补充,考虑使用HaZik提出的类型加载。
排除绑定重定向的任何特定原因能够解决当前的问题吗?Web和其他东西碰巧起作用(或者我很困惑,Ninject.MVC3位的源代码只是?(我知道一个没有任何限制的回答“只使用有约束力的条款”肯定也是错误的…)