新建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. 【转】HTML

    [转]HTML 一.html简介 1. 什么是 HTML? HTML 是用来描述网页的一种语言. HTML 指的是超文本标记语言 (Hyper Text Markup Language) HTML 不 ...

  2. HTML5在线预览PDF

    简介 PDF.js is a Portable Document Format (PDF) viewer that is built with HTML5. PDF.js is community-d ...

  3. Centos下替换yum源为阿里云源

    阿里云Linux安装镜像源地址:http://mirrors.aliyun.com/ 第一步:备份原镜像文件 mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum ...

  4. ansible笔记(6):常用模块之命令类模块

    ansible笔记():常用模块之命令类模块 command模块 command模块可以帮助我们在远程主机上执行命令 注意:使用command模块在远程主机中执行命令时,不会经过远程主机的shell处 ...

  5. 前端-----JavaScript 初识基础

    JavaScript的组成 JavaScript基础分为三个部分: ECMAScript:JavaScript的语法标准.包括变量.表达式.运算符.函数.if语句.for语句等. DOM:操作网页上的 ...

  6. postman 设置api_token,测试数据等操作

    在postman的环境变量中先设置好一个用户的token(事先你的数据库里面要有用户的这个数据),如下图: 然后在api请求的url 的head头部添加对应的token键名,value值用花括号{{t ...

  7. Oracle数据库的状态查询

    本文来源:huang_xw 的<Oracle数据库的状态查询> 1 状态查询 启动状态 SQL语句 结果 nomount select status from v$instance; ST ...

  8. Confluence 6 虚拟文件和文件夹

    在取消点赞事件中,你可能会遇到 WebDAV  客户端的问题或者不稳定的情况,你可以启用访问自动创建(虚拟)文件和文件夹. 备注: 在默认情况下,这个选项隐藏在 'WebDAV Configurati ...

  9. Confluence 6 让 Jira 应用停止发送通知到 Confluence

    你可能希望仅仅配置 Confluence 在 workbox 中仅仅显示自己的通知,禁用在 workbox 中显示从 Jira 来的通知.有可能这个 Jira 的应用已经通过应用链接功能正确链接到 C ...

  10. Java编程的分期步骤(一)

    日期:2018.8.12 星期一 博客期:005 不知不觉来到第五期了,先简单说一下Java环境!(虽然Java都自学完了才说....)首先,就是在网站上下载一个java包,之后把它下载到全英文的一个 ...