Mustafa
Mustafa

Reputation: 11

How to registr Automapper configuration class in global.asax?

I dont want to configure Automapper in global.asax. Instead of I want to create class that implemets Profile interface and registr this class in global.asax. But I dont know how to registr class in global.asax by it's direction. How can I do?

Upvotes: 1

Views: 2004

Answers (2)

Divyang Patel
Divyang Patel

Reputation: 988

You can register the configuration as below.

Profile class

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

AutoMap class

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();

Upvotes: 6

sander
sander

Reputation: 749

Have you tried (RTFM) reading the manual? Documentation

Upvotes: -3

Related Questions