Click here to Skip to main content
15,890,527 members
Articles / Programming Languages / C#
Tip/Trick

ASP.NET Core with automapper

Rate me:
Please Sign up or sign in to vote.
2.19/5 (4 votes)
10 Apr 2018CPOL 11.7K   6  
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.

C#
services.AddAutoMapper();

Result:

C#
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --