MVC AOP解决JsonResult返回json时间格式
新建JsonNetResult类:JsonResult
- public class JsonNetResult: JsonResult
- {
- public JsonNetResult()
- {
- Settings = new JsonSerializerSettings
- {
- ReferenceLoopHandling=ReferenceLoopHandling.Ignore,
- DateFormatString= "yyyy-MM-dd HH:mm:ss",
- ContractResolver=new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver()//json中属性开头字母小写的驼峰命名
- };
- }
- public JsonSerializerSettings Settings { get; private set; }
- public override void ExecuteResult(ControllerContext context)
- {
- if (context == null)
- throw new ArgumentNullException("context");
- //不允许GET请求
- if (this.JsonRequestBehavior == JsonRequestBehavior.DenyGet
- && string.Equals(context.HttpContext.Request.HttpMethod, "GET",
- StringComparison.OrdinalIgnoreCase))
- throw new InvalidOperationException("JSON GET is not allowed");
- HttpResponseBase response = context.HttpContext.Response;
- response.ContentType = string.IsNullOrEmpty(this.ContentType) ? "application/json" : this.ContentType;
- if (this.ContentEncoding != null)
- response.ContentEncoding = this.ContentEncoding;
- if (this.Data == null)
- return;
- var scriptSerializer = JsonSerializer.Create(this.Settings);
- scriptSerializer.Serialize(response.Output, this.Data);
- }
- }
新建JsonNetActionFilter过滤器:
- public class JsonNetActionFilter: IActionFilter
- {
- public void OnActionExecuted(ActionExecutedContext filterContext)
- {
- if (filterContext.Result is JsonResult
- && !(filterContext.Result is JsonNetResult))
- {
- JsonResult jsonResult = (JsonResult)filterContext.Result;
- JsonNetResult jsonNetResult = new JsonNetResult();
- jsonNetResult.ContentEncoding = jsonResult.ContentEncoding;
- jsonNetResult.ContentType = jsonResult.ContentType;
- jsonNetResult.Data = jsonResult.Data;
- jsonNetResult.JsonRequestBehavior = jsonResult.JsonRequestBehavior;
- jsonNetResult.MaxJsonLength = jsonResult.MaxJsonLength;
- jsonNetResult.RecursionLimit = jsonResult.RecursionLimit;
- filterContext.Result = jsonNetResult;
- }
- }
- public void OnActionExecuting(ActionExecutingContext filterContext)
- {
- }
- }
在Global中添加
- GlobalFilters.Filters.Add(new JsonNetActionFilter());
控制器:
- [HttpGet]
- public ActionResult TestJson()
- {
- return View();
- }
- [HttpPost]
- public ActionResult TestJson(FormCollection fc)
- {
- Dog dog = new Dog()
- {
- BirthDay = DateTime.Now,
- Id = 5,
- Name = "旺财"
- };
- return Json(dog);
- //return new JsonNetResult() { Data = dog };
- }
前端:
- <script type="text/javascript">
- $(function () {
- $("#btn1").click(function () {
- $.ajax({
- url: "/Home/TestJson",
- dataType: "json",
- type: "post",
- success: function (data) {
- alert(data.name);
- alert(data.birthDay);
- },
- error: function () {
- alert("ajax错误");
- }
- });
- });
- });
- </script>
MVC AOP解决JsonResult返回json时间格式的更多相关文章
- mvc使用JsonResult返回Json数据
mvc使用JsonResult返回Json数据 controller 中定义以下方法: public JsonResult UpdateSingle(int id, string actionNa ...
- OC处理.Net Json时间格式
通过服务器收到的json时间格式是/Date(xxxxxxxxxxxxx+xxxx)/,其中前半部分是自1970年的millionSecs,后半部是时区,我们需要对齐进行转换. 解决方式有两种,第一种 ...
- 解决springmvc返回json中文乱码
在pringmvc中通过设置@ResponseBody返回json乱码问题,这个问题上网找了很久,发现答案真是人云亦云,奉上我的解决方案: 解决方案一:需要导入 jackson-core-asl-1. ...
- JavaScriptSerializer 序列化json 时间格式
利用JavaScriptSerializer 序列化json 时间格式,得到的DateTime值值显示为“/Date(700000+0500)/”形式的JSON字符串,显然要进行转换 1.利用字符串直 ...
- spring boot 解决后台返回 json 到前台中文乱码之后出现返回json数据报错 500:no convertter for return value of type
问题描述 spring Boot 中文返回给浏览器乱码 解析成问号?? fastJson jackJson spring boot 新增配置解决后台返回 json 到前台中文乱码之后,出现返回json ...
- SpringMVC解决@ResponseBody返回Json的Date日期类型的转换问题
在做项目的时候,发现后台把Date类型的属性以json字符串的形式返回,前台拿不到转换后的日期格式,始终响应回去的都是long类型时间戳. 查阅资料之后找到解决方法: 方法一(在springmvc的x ...
- Spring MVC全局异常后返回JSON异常数据
问题: 当前项目是作为手机APP后台支持,使用spring mvc + mybaits + shiro进行开发.后台服务与手机端交互是发送JSON数据.如果后台发生异常,会直接返回异常页面,显示异常内 ...
- json时间格式的互换
c#代码 public class DateTimeUtil { /// <summary> /// 把json的时间格式还原-服务端 /// </summary> /// & ...
- ASP.NET Core 返回 Json DateTime 格式
ASP.NET Core 返回 Json 格式的时候,如果返回数据中有DateTime类型,如何自定义其格式呢?配置如下: services.AddMvc().AddJsonOptions(opt = ...
随机推荐
- python3-面向对象进阶(内置方法)
面向对象进阶: isinstance和issubclass 反射 __setattr__,__getattr,__delattr__ __setitem__,__getitem,__delitem__ ...
- LwIP Application Developers Manual11---Initializing lwIP
1.前言 2.Initialization for simple lwIP 查看doc/rawapi.txt来获得更多官方信息 #if NO_SYS /* Network interface vari ...
- ibevent 和 libev 提高网络应用性能【转】
转自:https://www.cnblogs.com/kunhu/p/3632285.html 构建现代的服务器应用程序需要以某种方法同时接收数百.数千甚至数万个事件,无论它们是内部请求还是网络连接, ...
- GCC 编译优化指南【转】
转自:http://www.jinbuguo.com/linux/optimize_guide.html 版权声明 本文作者是一位开源理念的坚定支持者,所以本文虽然不是软件,但是遵照开源的精神发布. ...
- lnmp 搭建 初试
#初始化环境检查 # uname -r -.el6.x86_64 # uname -m x86_64 #添加mysql用户 useradd -s /sbin/nologin mysql -M #下载安 ...
- 一个漂亮的php验证码类
一个漂亮的php验证码类(分享) 作者: 字体:[增加 减小] 类型:转载 下面小编就为大家分享一个漂亮的php验证码类.需要的朋友可以过来参考下 直接上代码: 复制代码 代码如下: //验证 ...
- ffmpeg 加载双语字幕
set infile=in.mp4 set subfile1=cn.srt set subfile2=en.srt set subvf1="subtitles=%subfile1%:forc ...
- Linq与Lambda常用查询语法
1.查询全部 2.按条件查询全部 3.去除重复 4.连接查询 between and 5.排序 6.分组
- css清除浏览器默认样式
css清除浏览器默认样式(代替 *{}) 将代码放入 css 文件,使用 link 引入. /* v2.0 | 20110126 http://meyerweb.com/eric/tools/css/ ...
- winfrom里面打印类似小票
首先在窗体上拖一个printDocument1控件和一个Button按钮,然后双击该控件的PrintPage事件,在事件里面复制下面代码: Pen blackPen = new Pen(Color.B ...