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>();
});
}
}
}
using AutoMapper;
using AutoMapperTest.Models; namespace AutoMapperTest.App_Start
{
public class MapperProfile : Profile
{
public MapperProfile()
{
CreateMap<StudentEntity, StudentOutput>();
}
}
}

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

using AutoMapperTest.App_Start;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing; namespace AutoMapperTest
{
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AutoMapperConfig.Config();
}
}
}

4、具体使用

        public JsonResult GetMapper()
{
//实例化实体List
List<StudentEntity> StudentList = new List<StudentEntity>();
//模拟数据
StudentList.Add(new StudentEntity
{
Id = ,
Age = ,
Gander = "boy",
Name = "WangZeLing",
Say = "Only the paranoid survive",
Score = 99M
});
//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; }
}
    public class StudentOutput
{
public string Name { get; set; }
public decimal Score { get; set; }
public string Say { get; set; }
}

附:AutoMapper GitHub

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. windows 下设置nginx负载均衡

    #user nobody; worker_processes ; #error_log logs/error.log; #error_log logs/error.log notice; #error ...

  2. App上架流程[利用Archive进行上传]

    作者 M_Lee2016.01.22 10:47 写了14852字,被32人关注,获得了49个喜欢 [iOS]App上架流程[利用Archive进行上传] 字数2186 阅读507 评论3 喜欢9 今 ...

  3. 使用xtrabackup备份innodb引擎的数据库

    innodb引擎的数据库可以使用mysqldump备份,如果表很大几十个G甚至上百G,显示用mysqldump备份会非常慢.然后使用xtrabackup 可以很快的在线备份innodb数据库.Inno ...

  4. HDU - 5658

    题意:给你一个字符串,给你Q次询问,每一次问你从l-r里有多少个回文串. 思路:len很小,所以直接遍历区间求就好了. /* gyt Live up to every day */ #include& ...

  5. scope属性

    scope属性 1.<bean>属性 2.作用:控制对象有效范围(单例.多例等) 3.<bean/> 标签对应的对象默认是单例的 3.1 无论获取多少次,都是同一个对象 4.s ...

  6. windows下Oracle数据库完全删除

    1.1   停止所有oracle的服务 1.2   删除安装路径 app及其下所有文件 1.3   删除注册表 regedit 进入 在下列列表中找到与oracle相关的注册表项删除 1.HKEY_L ...

  7. (13)How to stay calm when you know you'll be stressed

    https://www.ted.com/talks/daniel_levitin_how_to_stay_calm_when_you_know_you_ll_be_stressed/transcrip ...

  8. mysql (_mysql_exceptions.OperationalError) (1055, "Expression #1 of SELECT list is not in GROUP BY clause

    sudo gedit /etc/mysql/my.cnf在打开的my.cnf文件中添加 sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES 保存,退 ...

  9. Gym 100096D Guessing game

    Gym 100096D Guessing game 题面 Problem Description Byteman is playing a following game with Bitman. Bi ...

  10. kindeditor 富文本粘贴 图片

    <script type="text/javascript" src="../../Scripts/Plugins/kindeditor/kindeditor.js ...