Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7964473
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T05:54:01+00:00 2026-06-04T05:54:01+00:00

Where in my ASP.NET MVC application should I define my AutoMapper mappings? Mapper.CreateMap<User, UserViewModel>();

  • 0

Where in my ASP.NET MVC application should I define my AutoMapper mappings?

Mapper.CreateMap<User, UserViewModel>();

Presently I am defining these in the constructor of a BaseController, which all my Controlllers derive from. Is this the best place?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T05:54:02+00:00Added an answer on June 4, 2026 at 5:54 am

    I think it’s kind of late to answer the question, but maybe someone can use my answer.

    I use Ninject to resolve dependencies, so I’ve created Ninject Module for AutoMapper. Here is the code:

    public class AutoMapperModule : NinjectModule
    {
        public override void Load()
        {
            Bind<IConfiguration>().ToMethod(context => Mapper.Configuration);
            Bind<IMappingEngine>().ToMethod(context => Mapper.Engine);
    
            SetupMappings(Kernel.Get<IConfiguration>());
    
            Mapper.AssertConfigurationIsValid();
        }
    
        private static void SetupMappings(IConfiguration configuration)
        {
            IEnumerable<IViewModelMapping> mappings = typeof(IViewModelMapping).Assembly
                .GetExportedTypes()
                .Where(x => !x.IsAbstract &&
                            typeof(IViewModelMapping).IsAssignableFrom(x))
                .Select(Activator.CreateInstance)
                .Cast<IViewModelMapping>();
    
            foreach (IViewModelMapping mapping in mappings)
                mapping.Create(configuration);
        }
    }
    

    As you can see on load it scans assembly for implementaion of IViewModelMapping and then runs Create method.

    Here is the code of IViewModelMapping:

    interface IViewModelMapping
    {
        void Create(IConfiguration configuration);
    }
    

    Typical implementation of IViewModelMapping looks like this:

    public class RestaurantMap : IViewModelMapping
    {
        public void Create(IConfiguration configuration)
        {
            if (configuration == null)
                throw new ArgumentNullException("configuration");
    
            IMappingExpression<RestaurantViewModel, Restaurant> map =
                configuration.CreateMap<RestaurantViewModel, Restaurant>();
    // some code to set up proper mapping
    
            map.ForMember(x => x.Categories, o => o.Ignore());
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using AutoMapper in an ASP.NET MVC application. I was told that I should
I'm creating an intranet asp.net mvc application that everyone in the company should have
In my ASP.NET MVC application I want a user to add a value into
In an ASP.NET MVC application, how should the sending of emails be handled? I've
In a multi-tenant ASP.NET MVC application based on Rob Conery's MVC Storefront, should I
I am writing ASP.NET MVC application. When the user first logs in, the application
I have ASP.NET MVC application with action which should process posted XML data. At
I have an ASP.NET MVC application, when a user clicks on the submit button
I've got a asp.net mvc application with session state enabled (sqlSessionProvider). End user will
I have an ASP.NET MVC application that allows the user to upload a file

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.