【发布时间】:2015-01-23 22:03:43
【问题描述】:
我正在尝试在 ASP.NET MVC 5 应用程序中使用 Ninject,该应用程序使用 AutoMapper 将模型映射到视图模型,反之亦然。 不幸的是,我收到一条错误消息,指出缺少类型映射配置。
我创建了一个 Ninject 依赖解析器:
namespace MyNamespace.Infrastructure
{
public class NinjectDependencyResolver: IDependencyResolver
{
private IKernel kernel;
public NinjectDependencyResolver(IKernel kernelParam)
{
kernel = kernelParam;
AddBindings();
}
public object GetService(Type serviceType)
{
return kernel.TryGet(serviceType);
}
public IEnumerable<object> GetServices(Type serviceType)
{
return kernel.GetAll(serviceType);
}
private void AddBindings()
{
kernel.Bind<IMyBLL>().To<MyBLL>();
}
}
}
我用它来创建一个控制器:
namespace MyNamespace.Controllers
{
[Authorize]
public class HomeController : Controller
{
private IMyBLL _myBLL;
public HomeController(IMyBLL myBLLParam)
{
_myBLL = myBLLParam;
}
public PartialViewResult AddRecord()
{
return PartialView(new AddRecordViewModel());
}
[HttpPost]
public void AddRecord(AddRecordViewModel recordViewModel)
{
var record = Mapper.Map<Record>(recordViewModel);
_myBLL.AddRecord(record, User.Identity.Name);
}
}
}
全球.asax:
namespace MyNamespace.WebApplication
{
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
ApplicationUserManager.StartupAsync();
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
AutoMapperWebConfiguration.Configure();
}
}
}
这会调用 AutoMapper 配置:
namespace MyNamespace.WebApplication.Infrastructure
{
public static class AutoMapperWebConfiguration
{
public static void Configure()
{
Mapper.Initialize(cfg => cfg.AddProfile(new RecordProfile()));
}
}
public class RecordProfile : Profile
{
protected override void Configure()
{
Mapper.CreateMap<AddRecordViewModel, Record>().ReverseMap();
}
}
}
当我运行它时,我收到以下错误消息:
Missing type map configuration or unsupported mapping.
Mapping types:
AddRecordViewModel -> Record
MyNamespace.WebApplication.ViewModels.Home.AddRecordViewModel -> MyNamespace.Model.Record
Destination path:
Record
Source value:
MyNamespace.WebApplication.ViewModels.Home.AddRecordViewModel
我想念什么吗? 在我使用 Ninject 依赖解析器之前它运行良好。 现在它似乎没有找到映射。
编辑:
如果我将 Mapping Creation 直接添加到它工作的控制器方法中:
[HttpPost]
public void AddRecord(AddRecordViewModel recordViewModel)
{
Mapper.CreateMap<AddRecordViewModel, Record>().ReverseMap();
var record = Mapper.Map<Record>(recordViewModel);
_myBLL.AddRecord(record, User.Identity.Name);
}
映射本身以及模型和视图模型似乎不是问题。我猜该程序不知何故找不到映射。
即使我在控制器方法中调用 Auto Mapper Web 配置,它仍然有效:
public void AddRecord(AddRecordViewModel recordViewModel)
{
Infrastructure.AutoMapperWebConfiguration.Configure();
var record = Mapper.Map<Record>(recordViewModel);
_myBLL.AddRecord(record, User.Identity.Name);
}
【问题讨论】:
-
您定义了一个 AddRecordViewModel -> Record 但不是相反。您需要另一个映射。
-
我现在添加了第二个映射。不幸的是,我仍然收到相同的错误消息。程序似乎找不到映射。
-
你的
Record有虚拟财产吗? -
我就是有这个问题。我有其他与可查询扩展(项目到)一起使用的地图,但创建的另一个地图继续说没有映射,即使 MapperConfiguration 清楚地为这两种类型创建了一个地图。所有在同一台机器上运行的同一个项目中。请帮忙!
-
哇,我不知道 mapperConfig.CreateMapper() 正在创建唯一具有映射的实例!我必须使用注入来获取该映射器,我不能简单地使用 Automapper.Mapper.Map...
标签: c# dependency-injection asp.net-mvc-5 ninject automapper