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

1 asp.net-mvc automapper global-asax

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

Div*_*tel 6

您可以注册如下配置。

型材类

 public class UserProfile : Profile
    {
        public UserProfile()
        {
            this.CreateMap<UserDTO, User>();
            this.CreateMap<User, UserDTO>().ForMember(dest => dest.Company, opt => opt.Ignore()); ;
        }
    }
Run Code Online (Sandbox Code Playgroud)

自动地图类

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();
        }
    }
Run Code Online (Sandbox Code Playgroud)

全球.asax

AutoMap.RegisterMappings();
Run Code Online (Sandbox Code Playgroud)