1、安装automapper nuget包。

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

方法一、

  1. using AutoMapper;
  2. using AutoMapperTest.Models;
  3.  
  4. namespace AutoMapperTest.App_Start
  5. {
  6. public class AutoMapperConfig
  7. {
  8. public static void Config()
  9. {
  10. Mapper.Initialize(cfg =>
  11. {
  12. cfg.CreateMap<StudentEntity, StudentOutput>();
  13. });
  14. }
  15. }
  16. }

方法二、AddProfile方式

  1. using AutoMapper;
  2. using AutoMapperTest.Models;
  3.  
  4. namespace AutoMapperTest.App_Start
  5. {
  6. public class AutoMapperConfig
  7. {
  8. public static void Config()
  9. {
  10. Mapper.Initialize(cfg =>
  11. {
  12. cfg.AddProfile<MapperProfile>();
  13. });
  14. }
  15. }
  16. }
  1. using AutoMapper;
  2. using AutoMapperTest.Models;
  3.  
  4. namespace AutoMapperTest.App_Start
  5. {
  6. public class MapperProfile : Profile
  7. {
  8. public MapperProfile()
  9. {
  10. CreateMap<StudentEntity, StudentOutput>();
  11. }
  12. }
  13. }

3、在全局配置Global.asax中引用配置方法。

  1. using AutoMapperTest.App_Start;
  2. using System.Web.Mvc;
  3. using System.Web.Optimization;
  4. using System.Web.Routing;
  5.  
  6. namespace AutoMapperTest
  7. {
  8. public class MvcApplication : System.Web.HttpApplication
  9. {
  10. protected void Application_Start()
  11. {
  12. AutoMapperConfig.Config();
  13. }
  14. }
  15. }

4、具体使用

  1. public JsonResult GetMapper()
  2. {
  3. //实例化实体List
  4. List<StudentEntity> StudentList = new List<StudentEntity>();
  5. //模拟数据
  6. StudentList.Add(new StudentEntity
  7. {
  8. Id = ,
  9. Age = ,
  10. Gander = "boy",
  11. Name = "WangZeLing",
  12. Say = "Only the paranoid survive",
  13. Score = 99M
  14. });
  15. //AuotMapper具体使用方法 将List<StudentOutput>转换为List<StudentOutput>
  16. List<StudentOutput> Output = AutoMapper.Mapper.Map<List<StudentOutput>>(StudentList);
  17. return Json(Output, JsonRequestBehavior.AllowGet);
  18. }

附:实体类、Output类

  1. public class StudentEntity
  2. {
  3. public int Id { get; set; }
  4. public string Name { get; set; }
  5. public int Age { get; set; }
  6. public string Gander { get; set; }
  7. public decimal Score { get; set; }
  8. public string Say { get; set; }
  9. }
  1. public class StudentOutput
  2. {
  3. public string Name { get; set; }
  4. public decimal Score { get; set; }
  5. public string Say { get; set; }
  6. }

附:AutoMapper GitHub

  1. https://github.com/AutoMapper/AutoMapper

.Net Mvc AutoMapper简单使用的更多相关文章

  1. Nancy和MVC的简单对比

    Nancy和MVC的简单对比 在上一篇的.NET轻量级MVC框架:Nancy入门教程(一)——初识Nancy中,简单介绍了Nancy,并写了一个Hello,world.看到大家的评论,都在问Nancy ...

  2. [.Net Core] 在 Mvc 中简单使用日志组件

    在 Mvc 中简单使用日志组件 基于 .Net Core 2.0,本文只是蜻蜓点水,并非深入浅出. 目录 使用内置的日志组件 简单过渡到第三方组件 - NLog 使用内置的日志 下面使用控制器 Hom ...

  3. 转载 mvc:message-converters简单介绍 https://www.cnblogs.com/liaojie970/p/7736098.html

    mvc:message-converters简单介绍 说说@ResponseBody注解,很明显这个注解就是将方法的返回值作为reponse的body部分.我们进一步分析下这个过程涉及到的内容,首先就 ...

  4. 用Spring MVC开发简单的Web应用程序

    1 工具与环境 借助Eclipse4.3 + Maven3.0.3构建Java Web应用程序.使用Maven内置的servlet 容器jetty,不需手工集成Web服务器到Eclipse.还帮我们自 ...

  5. Spring MVC之简单入门

    一.Spring MVC简介: 1.什么是MVC 模型-视图-控制器(MVC)是一个众所周知的以设计界面应用程序为基础的设计模式.它主要通过分离模型(Model).视图(View)及控制器(Contr ...

  6. MVC CodeFirst简单的创建数据库(非常详细的步骤)

       最近在学习MVC的开发,相信有过开发经验的人初学一个新的框架时候的想法跟我一样最关心的就是这个框架如何架构,每个架构如何分工,以及最最关键的就是如何与数据库通信,再下来才是学习基础的页面设计啊等 ...

  7. EasyUI+MVC+EF简单用户管理Demo(问题及解决)

    写在前面 iframe-src EntityFramework版本 connectionStrings View.Action.页面跳转 EasyUI中DataGrid绑定 新增.修改和删除数据 效果 ...

  8. .NET轻量级MVC框架:Nancy入门教程(二)——Nancy和MVC的简单对比

    在上一篇的.NET轻量级MVC框架:Nancy入门教程(一)——初识Nancy中,简单介绍了Nancy,并写了一个Hello,world.看到大家的评论,都在问Nancy的优势在哪里?和微软的MVC比 ...

  9. (转)JAVA AJAX教程第四章—AJAX和MVC的简单结合

    这里我们再理解了AJAX后,开始来用实例感受AJAX的力量. 今天我最后要实现的效果,当鼠标放到图片上时会根据,会把数据库库里的数据读出,通过显示框显示出来.这个在很多网上商店都有用到这里效果,我们这 ...

随机推荐

  1. tensorflow初始化函数变更

    变量初始化函数改变 老版本:initialize_all_variables()(2017-03-02之后删除) 新版本:global_variables_initializer()

  2. Flex 排序 SortField and Sort

    部分代码 var arrayOfCat:ArrayCollection=outerDocument.getCagegory();   // 需要排序的数组 //创建SortField对象 var so ...

  3. hdu 6208(后缀自动机、或者AC自动机

    题意:给你n个字符串,问你是否存在一个字符串可以从中找到其他n-1个字符串. 思路:其实很简单,找到最长的那个字符串对他进行匹配,看是否能匹配到n-1个字符串. 可以用AC自动机或者后缀自动机做,但是 ...

  4. poj-2406(字符串hash)

    题目链接:传送门 思路:字符串hash,终止条件竟然判断错了,真是太大意了. #include<iostream> #include<cstdio> #include<c ...

  5. mysql的myBatis,主键自增设置

    方法一: insert id="insert" parameterType="Person" useGeneratedKeys="true" ...

  6. Ubuntu 16.04 搭建LAMP服务器环境流程

    http://www.linuxidc.com/Linux/2016-09/135629.htm [安装mysql时 只需安装 mysql-server无需安装mysql-client] mysql ...

  7. 详细解读 :java.sql.SQLException: Connection is read-only. Queries leading to data modification are not allowed,Java报错之Connection is read-only.

    问题分析: 实际开发项目中,进行insert的时候,产生这个问题是Spring框架的一个安全权限保护方法,对于方法调用的事物保护,一般配置如下: <!-- 事务管理 属性 --> < ...

  8. 开机logo以及两种修改开机动画方法

    Android开机画面总共有三屏 一.第一屏:开机logo 1.选张png格式的图片,在Linux任意下执行(安装工具): sudo apt-get install pnmtoplainpm 2.在所 ...

  9. ActiveMQ -5.9和jms-1.1源码下载

    ActiveMQ-5.9和jms-1.1源码下载:见附件

  10. C/C++ %s %d %u 基本概念与用法

    %d 十进制有符号整数 %u 十进制无符号整数 %f 浮点数 %s 字符串 %c 单个字符 %p 指针的值 %e 指数形式的浮点数 %x, %X 无符号以十六进制表示的整数 %0 无符号以八进制表示的 ...