我正在使用最新版本的AutoMapper。
我在我的automapper bootstrapper类中有Mapper.CreateMap<ArticleDto, EditViewModel>();
,在global.asax
中调用
以下是相同的完整列表:
public static class AutoMapperConfig
{
public static void Configure()
{
Mapper.Initialize(cfg => cfg.AddProfile(new AutomapperWebConfigurationProfile()));
Mapper.Initialize(cfg => cfg.AddProfile(new AutomapperServiceConfigurationProfile()));
}
}
public class AutomapperWebConfigurationProfile : Profile
{
protected override void Configure()
{
Mapper.CreateMap<CreateArticleViewModel, ArticleDto>()
.ForMember(dest => dest.Title, opts => opts.MapFrom(src => src.Title.Trim()))
.ForMember(dest => dest.PostBody, opts => opts.MapFrom(src => src.PostBody.Trim()))
.ForMember(dest => dest.Slug,
opts =>
opts.MapFrom(
src => string.IsNullOrWhiteSpace(src.Slug) ? src.Title.ToUrlSlug() : src.Slug.ToUrlSlug()))
.ForMember(dest => dest.Id, opt => opt.Ignore())
.ForMember(dest => dest.CreatedOn, opt => opt.Ignore())
.ForMember(dest => dest.Author, opt => opt.Ignore());
Mapper.CreateMap<ArticleDto, EditViewModel>()
.ForMember(dest => dest.Categories, opt => opt.Ignore());
Mapper.AssertConfigurationIsValid();
}
}
我花了将近两个小时的时间来确定下面映射的错误。
public class ArticleDto
{
public int Id { get; set; }
public string Slug { get; set; }
public string Title { get; set; }
public string PostBody { get; set; }
public DateTime CreatedOn { get; set; }
public bool IsPublished { get; set; }
public string Author { get; set; }
public List<string> ArticleCategories { get; set; }
}
public class EditViewModel
{
public int Id { get; set; }
public string Title { get; set; }
public string PostBody { get; set; }
public DateTime CreatedOn { get; set; }
public string Slug { get; set; }
public bool IsPublished { get; set; }
public IEnumerable<SelectListItem> Categories { get; set; }
public List<string> ArticleCategories { get; set; }
}
这是我控制器中的代码。
// Retrive ArticleDto from service
var articleDto = _engine.GetBySlug(slug);
// Convert ArticleDto to ViewModel and send it to view
var viewModel = Mapper.Map<EditViewModel>(articleDto);
将Dto转换为ViewModel失败。
任何人都可以帮我解决这个问题吗?这是例外的detials
Exception Details: AutoMapper.AutoMapperMappingException: Missing type map configuration or unsupported mapping.
Mapping types:
ArticleDto -> EditViewModel
NoobEngine.Dto.ArticleDto -> NoobEngineBlog.ViewModels.Article.EditViewModel
Destination path:
EditViewModel
Source value:
NoobEngine.Dto.ArticleDto
这是Mapper.AssertConfigurationIsValid();结果
Unmapped members were found. Review the types and members below.
Add a custom mapping expression, ignore, add a custom resolver, or modify the source/destination type
=============================================================================
ArticleDto -> EditViewModel (Destination member list)
NoobEngine.Dto.ArticleDto -> NoobEngineBlog.ViewModels.Article.EditViewModel (Destination member list)
Unmapped properties:
Categories
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: AutoMapper.AutoMapperConfigurationException:
Unmapped members were found. Review the types and members below.
Add a custom mapping expression, ignore, add a custom resolver, or modify the source/destination type
=============================================================================
ArticleDto -> EditViewModel (Destination member list)
NoobEngine.Dto.ArticleDto -> NoobEngineBlog.ViewModels.Article.EditViewModel (Destination member list)
Unmapped properties:
Categories
正如上面提到的错误,这是我如何更新我的映射
Mapper.CreateMap<ArticleDto, EditViewModel>()
.ForMember(dest => dest.Categories, opt => opt.Ignore());
即便如此,我也会得到与下面相同的错误。
An exception of type 'AutoMapper.AutoMapperMappingException' occurred in AutoMapper.dll but was not handled in user code
Missing type map configuration or unsupported mapping.
Mapping types:
ArticleDto -> EditViewModel
NoobEngine.Dto.ArticleDto -> NoobEngineBlog.ViewModels.Article.EditViewModel
Destination path:
EditViewModel
Source value:
NoobEngine.Dto.ArticleDto
答案 0 :(得分:3)
代码中存在两个问题:
第一个是导致问题的原因,第二个是我在配置中看到的内容。