.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的力量. 今天我最后要实现的效果,当鼠标放到图片上时会根据,会把数据库库里的数据读出,通过显示框显示出来.这个在很多网上商店都有用到这里效果,我们这 ...
随机推荐
- centos 6.5 配置ssh免登录
生成密匙: ssh-keygen -t rsa 会生成 id_rsa id_rsa.pub id_rsa:私匙 id_rsa.pub:公匙 配置当前机器免登录: cp id_rsa.pub auth ...
- 调用webservice时,产生android.os.NetworkOnMainThreadException错误
android.os.NetworkOnMainThreadException 网上搜索后知道是因为版本问题,在4.0之后在主线程里面执行Http请求都会报这个错,也许是怕Http请求时间太长造成程序 ...
- php服务器---IIS一些问题
配置网上很多博客都介绍过..这里不作详细说明了..将PHP目录(D:\PHP\PHP)下的php.ini-recommended或者php.ini-dist改名为php.ini,并找到extensio ...
- idea+tomcat 端口占用
ntelliJ IDEA和Tomcat整合注意事项(转) 这两天一直在学习IDEA这个开发工具,今天再整合tomcat的时候遇到了问题,运行时总是报错,说是8080端口被占用,把我就搞的郁闷了,我就尝 ...
- Windows 8.1 新控件和功能:
http://msdn.microsoft.com/zh-cn/library/windows/apps/bg182878.aspx#five 将 XAML 树呈现为位图: 适用于 Windows 8 ...
- ubuntu16下的/etc/resolv.conf重置的解决方案
此文件存放了网络网关信息,重启后会刷新,刷新来源有两个可能 一个是根据文件中的resolvconf目录下的resolv.conf.d目录下的base文件 另一个来源是/etc/network/inte ...
- rails 表单中默认值
在表单中加入默认提示值,如(email@email.com): <div class="field"> <%= form.label :email,"E ...
- 网络编程之基于tcp和udp的套接字
一 udp协议网络编程 DNS:将域名解析成ip地址 SOCK_DGRAM:数据报协议,也是udp协议 udp协议的网络编程的一些用法: recvfrom:接收消息,接收的时一个元组,元组里面的元 ...
- ajax +jsp+iframe无刷新上传文件[转]
http://hi.baidu.com/zj360202/blog/item/f23e3711f929c774cb80c475.html ajax jsp 无刷新上传文件 2009-10-26 16: ...
- ACM-ICPC 2018 徐州赛区网络预赛 A Hard to prepare
https://nanti.jisuanke.com/t/31453 题目大意: 有n个人坐成一圈,然后有\(2^k\)种颜色可以分发给每个人,每个人可以收到相同的颜色,但是相邻两个人的颜色标号同或不 ...