1. Install the AutoMapper nuget package.
2. Create a new AutoMapper configuration class and implement a static configuration method.
using AutoMapper; using Automappertest.models; namespace automappertest.app_start{ publicclass automapperconfig { Public Static void Config () { = = { cfg. Createmap<studententity, studentoutput>(); }); }}
3. Reference the configuration method in the global configuration global.asax.
using Automappertest.app_start; using SYSTEM.WEB.MVC; using System.Web.Optimization; using System.Web.Routing; namespace automappertest{ publicclass MvcApplication:System.Web.HttpApplication { protectedvoid Application_Start () { Automapperconfig.config (); }}}
4. Specific Use
PublicJsonresult Getmapper () {//instantiating a list of entitiesList<studententity> studentlist =NewList<studententity>(); //Analog DataStudentlist.add (Newstudententity {Id=1, Age= A, Gander=" Boy", Name="wangzeling", Say="Only the paranoid survive", score=99M}); //Auotmapper How to convert list<studentoutput> to list<studentoutput> using specific methodslist<studentoutput> Output = automapper.mapper.map<list<studentoutput>>(studentlist); returnJson (Output, jsonrequestbehavior.allowget); }
Attached: entity class, Output class
Public classstudententity { Public intId {Get;Set; } Public stringName {Get;Set; } Public intAge {Get;Set; } Public stringGander {Get;Set; } Public decimalScore {Get;Set; } Public stringSay {Get;Set; } }
Public class Studentoutput { publicstringgetset;} Public decimal Get Set ; } Public string Get Set ; } }
Attached: AutoMapper GitHub
HTTPS://github.com/automapper/automapper
. Net MVC AutoMapper Simple to use