.Net Mvc AutoMapper简单使用
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简单使用的更多相关文章
- Nancy和MVC的简单对比
Nancy和MVC的简单对比 在上一篇的.NET轻量级MVC框架:Nancy入门教程(一)——初识Nancy中,简单介绍了Nancy,并写了一个Hello,world.看到大家的评论,都在问Nancy ...
- [.Net Core] 在 Mvc 中简单使用日志组件
在 Mvc 中简单使用日志组件 基于 .Net Core 2.0,本文只是蜻蜓点水,并非深入浅出. 目录 使用内置的日志组件 简单过渡到第三方组件 - NLog 使用内置的日志 下面使用控制器 Hom ...
- 转载 mvc:message-converters简单介绍 https://www.cnblogs.com/liaojie970/p/7736098.html
mvc:message-converters简单介绍 说说@ResponseBody注解,很明显这个注解就是将方法的返回值作为reponse的body部分.我们进一步分析下这个过程涉及到的内容,首先就 ...
- 用Spring MVC开发简单的Web应用程序
1 工具与环境 借助Eclipse4.3 + Maven3.0.3构建Java Web应用程序.使用Maven内置的servlet 容器jetty,不需手工集成Web服务器到Eclipse.还帮我们自 ...
- Spring MVC之简单入门
一.Spring MVC简介: 1.什么是MVC 模型-视图-控制器(MVC)是一个众所周知的以设计界面应用程序为基础的设计模式.它主要通过分离模型(Model).视图(View)及控制器(Contr ...
- MVC CodeFirst简单的创建数据库(非常详细的步骤)
最近在学习MVC的开发,相信有过开发经验的人初学一个新的框架时候的想法跟我一样最关心的就是这个框架如何架构,每个架构如何分工,以及最最关键的就是如何与数据库通信,再下来才是学习基础的页面设计啊等 ...
- EasyUI+MVC+EF简单用户管理Demo(问题及解决)
写在前面 iframe-src EntityFramework版本 connectionStrings View.Action.页面跳转 EasyUI中DataGrid绑定 新增.修改和删除数据 效果 ...
- .NET轻量级MVC框架:Nancy入门教程(二)——Nancy和MVC的简单对比
在上一篇的.NET轻量级MVC框架:Nancy入门教程(一)——初识Nancy中,简单介绍了Nancy,并写了一个Hello,world.看到大家的评论,都在问Nancy的优势在哪里?和微软的MVC比 ...
- (转)JAVA AJAX教程第四章—AJAX和MVC的简单结合
这里我们再理解了AJAX后,开始来用实例感受AJAX的力量. 今天我最后要实现的效果,当鼠标放到图片上时会根据,会把数据库库里的数据读出,通过显示框显示出来.这个在很多网上商店都有用到这里效果,我们这 ...
随机推荐
- tensorflow初始化函数变更
变量初始化函数改变 老版本:initialize_all_variables()(2017-03-02之后删除) 新版本:global_variables_initializer()
- Flex 排序 SortField and Sort
部分代码 var arrayOfCat:ArrayCollection=outerDocument.getCagegory(); // 需要排序的数组 //创建SortField对象 var so ...
- hdu 6208(后缀自动机、或者AC自动机
题意:给你n个字符串,问你是否存在一个字符串可以从中找到其他n-1个字符串. 思路:其实很简单,找到最长的那个字符串对他进行匹配,看是否能匹配到n-1个字符串. 可以用AC自动机或者后缀自动机做,但是 ...
- poj-2406(字符串hash)
题目链接:传送门 思路:字符串hash,终止条件竟然判断错了,真是太大意了. #include<iostream> #include<cstdio> #include<c ...
- mysql的myBatis,主键自增设置
方法一: insert id="insert" parameterType="Person" useGeneratedKeys="true" ...
- Ubuntu 16.04 搭建LAMP服务器环境流程
http://www.linuxidc.com/Linux/2016-09/135629.htm [安装mysql时 只需安装 mysql-server无需安装mysql-client] mysql ...
- 详细解读 :java.sql.SQLException: Connection is read-only. Queries leading to data modification are not allowed,Java报错之Connection is read-only.
问题分析: 实际开发项目中,进行insert的时候,产生这个问题是Spring框架的一个安全权限保护方法,对于方法调用的事物保护,一般配置如下: <!-- 事务管理 属性 --> < ...
- 开机logo以及两种修改开机动画方法
Android开机画面总共有三屏 一.第一屏:开机logo 1.选张png格式的图片,在Linux任意下执行(安装工具): sudo apt-get install pnmtoplainpm 2.在所 ...
- ActiveMQ -5.9和jms-1.1源码下载
ActiveMQ-5.9和jms-1.1源码下载:见附件
- C/C++ %s %d %u 基本概念与用法
%d 十进制有符号整数 %u 十进制无符号整数 %f 浮点数 %s 字符串 %c 单个字符 %p 指针的值 %e 指数形式的浮点数 %x, %X 无符号以十六进制表示的整数 %0 无符号以八进制表示的 ...