我不想在global.asax中配置Automapper。而不是我想创建实现Profile接口的类并在global.asax中注册该类。但是我不知道如何通过global.asax的方向注册该类。我该怎么办?
您是否尝试过(RTFM)阅读本手册? Documentation
您可以按以下方式注册配置。
个人资料类别
public class UserProfile : Profile
{
public UserProfile()
{
this.CreateMap<UserDTO, User>();
this.CreateMap<User, UserDTO>().ForMember(dest => dest.Company, opt => opt.Ignore()); ;
}
}
AutoMap类
public static class AutoMap
{
public static IMapper Mapper { get; set; }
public static void RegisterMappings()
{
var mapperConfiguration = new MapperConfiguration(
config =>
{
config.AddProfile<UserProfile>();
});
Mapper = mapperConfiguration.CreateMapper();
}
}
Global.asax
AutoMap.RegisterMappings();