Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

ASP.NET Core with automapper

0.00/5 (No votes)
10 Apr 2018 1  
How to setup net core with automapper

Introduction

This article should help you to wire up automapper into your project.
I am using .NET Core 2.0 and AutoMapper 6.1.1.

Install

First, install SyrianBallaS.AutoMapper.Extensions.Microsoft.DependencyInjection.Signed nuget package. (I am using version 3.2.0.)

Code

In Startup.cs, add this line to ConfigureServices method.

services.AddAutoMapper();

Result:

Startup.cs

using AutoMapper;

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc();
    services.AddAutoMapper();
}

 

Now, you just have to create your Mapping profiles.

Here is an example of mine:

using AutoMapper;

public class MappingProfile : Profile
{
    public MappingProfile()
    {
        CreateMap<V1Response, ConfigurationResponse>()
           .ForMember(a => a.ModelId, b=> b.MapFrom(c=> c.ModelName));
     }
}

Mapping

And here is how you can map your objects.

Here is an example of mine:

var result = Mapper.Map<V1Response, ConfigurationResponse>(v1);

Summary

If you want to know how to setup automapper for ASP MVC, have a look at my previous article.
 

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here