.NetMvcAutoMapper简单使用-飞外

1、安装automapper nuget包。

2、新建一个AutoMapper配置类并实现一个静态配置方法。

方法一、

using AutoMapper;using AutoMapperTest.Models;namespace AutoMapperTest.App_Start public class AutoMapperConfig public static void Config() Mapper.Initialize(cfg =  cfg.CreateMap StudentEntity, StudentOutput ();}

方法二、AddProfile方式

using AutoMapper;using AutoMapperTest.Models;namespace AutoMapperTest.App_Start public class AutoMapperConfig public static void Config() Mapper.Initialize(cfg =  cfg.AddProfile MapperProfile ();}

//AuotMapper具体使用方法 将List StudentOutput 转换为List StudentOutput List StudentOutput Output = AutoMapper.Mapper.Map List StudentOutput (StudentList); return Json(Output, JsonRequestBehavior.AllowGet); }

附:实体类、Output类

 public class StudentEntity public int Id { get; set; } public string Name { get; set; } public int Age { get; set; } public string Gander { get; set; } public decimal Score { get; set; } public string Say { get; set; } }