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. STL基础2:vector中使用结构体

    #include <iostream> #include <vector> #include <numeric> #include <algorithm> ...

  2. 进入快速通道的委托(深入理解c#)

    1.方法组:所有的名称相同的重载方法合在一起就成为一个方法组. 2.协变性和逆变性: 协变性指的是——泛型类型参数可以从一个派生类隐式转化为基类. 逆变性指的是——泛型类型参数可以从一个基类隐式转化为 ...

  3. MyBatis中的缓存1

    1.应用程序和数据库交互的过程是一个相对比较耗时的过程 2.缓存存在的意义:让应用程序减少对数据库的访问,提升程序运行的xiaolv 3.MyBatis中默认SqlSession缓存开启 3.1  同 ...

  4. Opencv基本数据类型

    1.OpenCV中数据类型和常用数据类型对应 Mat<uchar>                            CV_8U Mat<char>            ...

  5. win/mac平台搭建ionic开发环境教程(转)

    出处:http://www.ionic-china.com/doc/ionic-winmac.html#preface 前言 ionic中文网为大家准备了绿色版的nodejs和androidSDK以及 ...

  6. 微信小程序使用三元表达式切换图片

    1.data里定义切换图片的地址和切换的标识 data:{ show:true, yes:"http://101.89.144.168:9090//files/jk/yd/images/in ...

  7. FontAwesome 4.7.0 中完整的675个图标样式CSS参考

    FontAwesome 4.7.0 中完整的675个图标样式CSS参考 用法:首先引入CSS文件:<link href="https://maxcdn.bootstrapcdn.com ...

  8. php 16进制颜色代码转换为rgba,rgb格式

    <?php $rgb = hex2rgba('#FFFFFF', false, true); echo 'rgb: '.$rgb[0].','; echo $rgb[1].','; echo $ ...

  9. js读取后端写入cookie出现乱码

    设置字符编码集即可 Cookie cookie = new Cookie("user",URLEncoder.encode(nMessage, "UTF-8") ...

  10. 手机开发-IOS

    IOS 语言.Object-C,苹果公司收购的语言,专用于IOS开发,是C语言的超集,面向对象的. 开发环境.一是XCode,是苹果的IDE,提供了控件.二是Instruments,测试性能用,收集显 ...