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. callable与runable区别?switch char ?sql只查是否存在,sql复制表 ?反射 ? spring mvc 和spring 上下文区别?

    中化技术部  2018.4.16 1. callable 和 thread 区别 实现Callable接口的线程能返回执行结果,而Runable 不可以 . Callable 的call方法允许抛出异 ...

  2. 好像leeceode题目我的博客太长了,需要重新建立一个. leecode刷题第二个

    376. Wiggle Subsequence               自己没想出来,看了别人的分析. 主要是要分析出升序降序只跟临近的2个决定.虽然直觉上不是这样. 455. 分发饼干     ...

  3. Silverlight保存大数据到WCF出现Not Found问题

    Silveright调用WCF保存大数据时,直接在客户端报“ 远程服务器返回了错误:Not Found ”问题,少量是没问题,如下图: 后面在这个文章中找到解决办法: http://www.cnblo ...

  4. Quartz(强大的定时器)

    1.关于Quartz的配置文件说明 # # Quartz会优先读取项目下我们自定义这个quartz.properties配置文件 否则会去读取quartzjar包下org.quatrz包# 下面的那个 ...

  5. kafka 支持发布订阅

    概述 一般消息队列的是实现是支持两种模式的,即点对点,还有一种是topic发布订阅者模式,比如ACTIVEMQ.KAFKA也支持这两种模式,但是实现的原理不一样. KAFKA 的消息被读取后,并不是马 ...

  6. Java查看项目目录以及导入项目

    1.查看项目目录 右击项目,选择properties,选择resource,查看location 2.导入已有项目 (1)右击项目 ,选择import (2)在弹出的窗口中选择Existing pro ...

  7. 对象序列化:pickle和shelve

    import pickle class DVD: def __init__(self,tilte,year=None,duration=None,director_id=None): self.tit ...

  8. jQuery警告/确认/提示弹出对话框效果(替换传统JavaScript下的提示框)

    http://www.51xuediannao.com/js/jquery/jquery_tsk/ http://www.jq22.com/demo/jqueryConfirm20160413/

  9. Win10通知区域图标设置;windows10系统图标合并;Windows10系统通知合并

    1.一直喜欢Windows7的通知图标合并 2.通过查阅找到方法 a.输入命令语句   win+R  :shell:::{05d7b0f4-2121-4eff-bf6b-ed3f69b894d9} 回 ...

  10. 怎样导入现有的NiosII工程

    查找路径