如何在global.asax中注册Automapper配置类?

问题描述 投票:0回答:2

我不想在global.asax中配置Automapper。而不是我想创建实现Profile接口的类并在global.asax中注册该类。但是我不知道如何通过global.asax的方向注册该类。我该怎么办?

asp.net-mvc automapper global-asax
2个回答
0
投票

您是否尝试过(RTFM)阅读本手册? Documentation


0
投票

您可以按以下方式注册配置。

个人资料类别

 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();
© www.soinside.com 2019 - 2024. All rights reserved.