新建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时间格式的更多相关文章

  1. mvc使用JsonResult返回Json数据

    mvc使用JsonResult返回Json数据   controller 中定义以下方法: public JsonResult UpdateSingle(int id, string actionNa ...

  2. OC处理.Net Json时间格式

    通过服务器收到的json时间格式是/Date(xxxxxxxxxxxxx+xxxx)/,其中前半部分是自1970年的millionSecs,后半部是时区,我们需要对齐进行转换. 解决方式有两种,第一种 ...

  3. 解决springmvc返回json中文乱码

    在pringmvc中通过设置@ResponseBody返回json乱码问题,这个问题上网找了很久,发现答案真是人云亦云,奉上我的解决方案: 解决方案一:需要导入 jackson-core-asl-1. ...

  4. JavaScriptSerializer 序列化json 时间格式

    利用JavaScriptSerializer 序列化json 时间格式,得到的DateTime值值显示为“/Date(700000+0500)/”形式的JSON字符串,显然要进行转换 1.利用字符串直 ...

  5. spring boot 解决后台返回 json 到前台中文乱码之后出现返回json数据报错 500:no convertter for return value of type

    问题描述 spring Boot 中文返回给浏览器乱码 解析成问号?? fastJson jackJson spring boot 新增配置解决后台返回 json 到前台中文乱码之后,出现返回json ...

  6. SpringMVC解决@ResponseBody返回Json的Date日期类型的转换问题

    在做项目的时候,发现后台把Date类型的属性以json字符串的形式返回,前台拿不到转换后的日期格式,始终响应回去的都是long类型时间戳. 查阅资料之后找到解决方法: 方法一(在springmvc的x ...

  7. Spring MVC全局异常后返回JSON异常数据

    问题: 当前项目是作为手机APP后台支持,使用spring mvc + mybaits + shiro进行开发.后台服务与手机端交互是发送JSON数据.如果后台发生异常,会直接返回异常页面,显示异常内 ...

  8. json时间格式的互换

    c#代码 public class DateTimeUtil { /// <summary> /// 把json的时间格式还原-服务端 /// </summary> /// & ...

  9. ASP.NET Core 返回 Json DateTime 格式

    ASP.NET Core 返回 Json 格式的时候,如果返回数据中有DateTime类型,如何自定义其格式呢?配置如下: services.AddMvc().AddJsonOptions(opt = ...

随机推荐

  1. spring源码学习2

    spring总览 从入口看起 我们用spring时会用ClassPathXmlApplicationContext来加载spring配置文件,就从它开始吧. 1.双击shhift,输入ClassPat ...

  2. SpringCloud概述

    ⒈官网说明 SpringCloud是基于SpringBoot提供了一套微服务解决方案,包括服务注册与发现.配置中心.全链路监控.服务网关.负载均衡.熔断器等组件,除了基于Netflix的开源组件做高度 ...

  3. 二、Java神经网络框架Neuroph的使用和架构分析

    一.使用Neuroph Studio构造感知机处理逻辑与 新建项目 接着,输入名字和地址,点击“完成” 在工程的神经网络文件下新建神经网络 准备训练数据 开始训练 误差展示 也可以测试神经元 或者输入 ...

  4. 【C++】一篇文章,让你不再害怕指针

    在C++中,比较难以理解的就是指针,最常用的也是指针.这篇文章,结合我的所学,所看,来谈谈C++中的指针 指针是什么 指针是一个特殊的变量,指向内存中的一个地址.它具有四个要素: 指针类型:即指针本身 ...

  5. go语言中的运算符^,&

    一.^运算符 1.作为二元运算符 ^作二元运算符就是异或,包括符号位在内,相同为0,不相同为1 规则:1^1 =0, 0^0=0,1^0=1,0^1=1 事例: (1)0001 0100 ^ 0000 ...

  6. Euclideanloss_layer层解析

    这里说一下euclidean_loss_layer.cpp关于该欧式loss层的解析,代码如下: #include <vector> #include "caffe/layers ...

  7. c 中打印格式%g

    C语言中打印float或double类型最常用的是%f格式,最近看书时看到有使用%g格式打印. %f  表示按浮点数的格式打印. 小数点后固定6位 %e 表示以指数形式的浮点数格式输出. %g 表示自 ...

  8. Sublime Text3 C++ 设置

    系统环境: 首先是 C++ 的环境设置, 先下载 CodeBlocks, 安装目录下有 MinGW, 直接复制到 C 盘下, 再在电脑中添加 环境变量(PATH). Sublime Text3 设置: ...

  9. tcpdump详解

    tcpdump -i eth1 'host 121.14.84.221 and greater 76' -Ap -v -s10000 抓取 eth1 和 121.14.84.221 上的所有长度大于7 ...

  10. 【进阶3-4期】深度解析bind原理、使用场景及模拟实现(转)

    这是我在公众号(高级前端进阶)看到的文章,现在做笔记  https://github.com/yygmind/blog/issues/23 bind() bind() 方法会创建一个新函数,当这个新函 ...