新建JsonNetResult类:JsonResult

  1. public class JsonNetResult: JsonResult
  2. {
  3. public JsonNetResult()
  4. {
  5. Settings = new JsonSerializerSettings
  6. {
  7. ReferenceLoopHandling=ReferenceLoopHandling.Ignore,
  8. DateFormatString= "yyyy-MM-dd HH:mm:ss",
  9. ContractResolver=new Newtonsoft.Json.Serialization.CamelCasePropertyNamesContractResolver()//json中属性开头字母小写的驼峰命名
  10. };
  11. }
  12. public JsonSerializerSettings Settings { get; private set; }
  13.  
  14. public override void ExecuteResult(ControllerContext context)
  15. {
  16. if (context == null)
  17. throw new ArgumentNullException("context");
  18. //不允许GET请求
  19. if (this.JsonRequestBehavior == JsonRequestBehavior.DenyGet
  20. && string.Equals(context.HttpContext.Request.HttpMethod, "GET",
  21. StringComparison.OrdinalIgnoreCase))
  22. throw new InvalidOperationException("JSON GET is not allowed");
  23.  
  24. HttpResponseBase response = context.HttpContext.Response;
  25. response.ContentType = string.IsNullOrEmpty(this.ContentType) ? "application/json" : this.ContentType;
  26.  
  27. if (this.ContentEncoding != null)
  28. response.ContentEncoding = this.ContentEncoding;
  29. if (this.Data == null)
  30. return;
  31. var scriptSerializer = JsonSerializer.Create(this.Settings);
  32. scriptSerializer.Serialize(response.Output, this.Data);
  33. }
  34. }

  新建JsonNetActionFilter过滤器:

  1. public class JsonNetActionFilter: IActionFilter
  2. {
  3. public void OnActionExecuted(ActionExecutedContext filterContext)
  4. {
  5. if (filterContext.Result is JsonResult
  6. && !(filterContext.Result is JsonNetResult))
  7. {
  8. JsonResult jsonResult = (JsonResult)filterContext.Result;
  9. JsonNetResult jsonNetResult = new JsonNetResult();
  10.  
  11. jsonNetResult.ContentEncoding = jsonResult.ContentEncoding;
  12. jsonNetResult.ContentType = jsonResult.ContentType;
  13. jsonNetResult.Data = jsonResult.Data;
  14. jsonNetResult.JsonRequestBehavior = jsonResult.JsonRequestBehavior;
  15. jsonNetResult.MaxJsonLength = jsonResult.MaxJsonLength;
  16. jsonNetResult.RecursionLimit = jsonResult.RecursionLimit;
  17.  
  18. filterContext.Result = jsonNetResult;
  19. }
  20. }
  21.  
  22. public void OnActionExecuting(ActionExecutingContext filterContext)
  23. {
  24.  
  25. }
  26. }

  在Global中添加

  1. GlobalFilters.Filters.Add(new JsonNetActionFilter());

  控制器:

  1. [HttpGet]
  2. public ActionResult TestJson()
  3. {
  4. return View();
  5. }
  6. [HttpPost]
  7. public ActionResult TestJson(FormCollection fc)
  8. {
  9. Dog dog = new Dog()
  10. {
  11. BirthDay = DateTime.Now,
  12. Id = 5,
  13. Name = "旺财"
  14. };
  15. return Json(dog);
  16. //return new JsonNetResult() { Data = dog };
  17. }

  前端:

  1. <script type="text/javascript">
  2. $(function () {
  3. $("#btn1").click(function () {
  4. $.ajax({
  5. url: "/Home/TestJson",
  6. dataType: "json",
  7. type: "post",
  8. success: function (data) {
  9. alert(data.name);
  10. alert(data.birthDay);
  11. },
  12. error: function () {
  13. alert("ajax错误");
  14. }
  15. });
  16. });
  17. });
  18. </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. python3-面向对象进阶(内置方法)

    面向对象进阶: isinstance和issubclass 反射 __setattr__,__getattr,__delattr__ __setitem__,__getitem,__delitem__ ...

  2. LwIP Application Developers Manual11---Initializing lwIP

    1.前言 2.Initialization for simple lwIP 查看doc/rawapi.txt来获得更多官方信息 #if NO_SYS /* Network interface vari ...

  3. ibevent 和 libev 提高网络应用性能【转】

    转自:https://www.cnblogs.com/kunhu/p/3632285.html 构建现代的服务器应用程序需要以某种方法同时接收数百.数千甚至数万个事件,无论它们是内部请求还是网络连接, ...

  4. GCC 编译优化指南【转】

    转自:http://www.jinbuguo.com/linux/optimize_guide.html 版权声明 本文作者是一位开源理念的坚定支持者,所以本文虽然不是软件,但是遵照开源的精神发布. ...

  5. lnmp 搭建 初试

    #初始化环境检查 # uname -r -.el6.x86_64 # uname -m x86_64 #添加mysql用户 useradd -s /sbin/nologin mysql -M #下载安 ...

  6. 一个漂亮的php验证码类

    一个漂亮的php验证码类(分享)   作者: 字体:[增加 减小] 类型:转载 下面小编就为大家分享一个漂亮的php验证码类.需要的朋友可以过来参考下   直接上代码: 复制代码 代码如下: //验证 ...

  7. ffmpeg 加载双语字幕

    set infile=in.mp4 set subfile1=cn.srt set subfile2=en.srt set subvf1="subtitles=%subfile1%:forc ...

  8. Linq与Lambda常用查询语法

    1.查询全部 2.按条件查询全部 3.去除重复 4.连接查询    between and 5.排序 6.分组

  9. css清除浏览器默认样式

    css清除浏览器默认样式(代替 *{}) 将代码放入 css 文件,使用 link 引入. /* v2.0 | 20110126 http://meyerweb.com/eric/tools/css/ ...

  10. winfrom里面打印类似小票

    首先在窗体上拖一个printDocument1控件和一个Button按钮,然后双击该控件的PrintPage事件,在事件里面复制下面代码: Pen blackPen = new Pen(Color.B ...