.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的力量. 今天我最后要实现的效果,当鼠标放到图片上时会根据,会把数据库库里的数据读出,通过显示框显示出来.这个在很多网上商店都有用到这里效果,我们这 ...
随机推荐
- php emoji mysql保存和搜索
MySQL版本>=5.5.3 表字符集: utf8mb4 解决保存 排序规格: utf8mb4_bin 解决搜索 PHP: set names utf8mb4 操作系统: WIN10 MAC
- 浅谈 [Ljava.lang.Object 异常
http://blog.csdn.net/goodleiwei/article/details/7059567 主要原因:取出的是对象的数组,需要遍历单个的对象并获取想用的属性值
- 通用Mapper
原理是:拦截器 1.假设:使用MyBatis只需要定义Mapper接口,无需编写Mapper.xml文件 如果实现无需编写Mapper.xml文件,我们必须要实现动态拼接SQL 如何实现动态拼接SQL ...
- 格式化输出python
一.格式化输出 1.实例 name = input("Name:") age = input("Age:") job = input("Job:&qu ...
- hdu 1540(线段树区间合并)
题目链接:传送门 参考文章:传送门 题意:n个数字初始连在一条线上,有三种操作, D x表示x号被摧毁: R 表示恢复剩下的通路 Q表示查询标号为x所在的串的最长长度. 思路:线段树的区间合并. #i ...
- 枚举子窗口EnumChildWindows()的应用
1.EnumChildWindows()函数的作用枚举子窗口(按顺序调用回调函数,并将子窗口的句柄传递给了回调函数).函数原型: BOOL WINAPI EnumChildWindows( HWND ...
- Day1-Python基础--数据类型
距离上次更新,已经一月有余.说明学习状态不好,且滞后严重.第二模块也滞后5周之多,可能学习方法不对,有点凌乱,导致写作业时思路还是打不开,再一个是练习的太少了吧,以后得多挤挤时间来了.目前到了这个年纪 ...
- 如何使用vs进行代码比较
当我们在进行团队合作开始项目时,有时候不仅自己要写代码还需要修改bug,当我们修改代码以后,为了保持代码库中代码的整洁美观和一直性,有些误操作,比如多一个或多个空格,多一行,少一行,格式对齐等,这样的 ...
- 25个Linux相关的网站
下面是25个最具有影响力,也是最重要的Linux网站,这些网站提供了Linux的分发包,软件,文件,新闻,以及其它所有的关于Linux的东西.关于Linux的分发包历史,可以看看本站的这篇文章< ...
- 用Dagger2在Android中实现依赖注入
依赖注入这个模式(模式已经用烂了,这里再烂一次)是用来给应用的各部分解耦的.使应用开发更加可扩展,更容易维护.通过本文你会学到如何使用Dagger2来处理依赖. 简介 如果以对象需要另外的一个对象才能 ...