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 = ...
随机推荐
- 尚硅谷Oracle教程-学习笔记2
控制用户权限 1.创建用户 1) SQL> create user atguigu01 2 identified by atguigu01; User created 2) SQL> gr ...
- ARMV7-M数据手册---Part A :Application Level Architecture---A1 Introduction
1.前言 本章主要介绍了ARMV7体系结构及其定义的属性,以及本手册定义的ARMV7M属性. 主要包括: ARMV7体系结构和属性 ARMV7M属性 ARMV7M扩展 2. ARMV7体系结构和属性 ...
- Nginx安装及配置详解【转】
nginx概述 nginx是一款自由的.开源的.高性能的HTTP服务器和反向代理服务器:同时也是一个IMAP.POP3.SMTP代理服务器:nginx可以作为一个HTTP服务器进行网站的发布处理,另外 ...
- Jetbrain系列软件配置文件同步
https://intellij-support.jetbrains.com/hc/en-us/articles/206544519-Directories-used-by-the-IDE-to-st ...
- Linux下编写和加载 .ko 文件(驱动模块文件)
一..ko 文件介绍 .ko文件是kernel object文件(内核模块),该文件的意义就是把内核的一些功能移动到内核外边, 需要的时候插入内核,不需要时卸载. 二.优点 (1)这样可以缩小内核体积 ...
- AngularJs 刷新页面
第一种: AngularJs 刷新页面可采用下面的方式:首先先在控制器中注册$window,然后定义函数$scope.reloadRoute,在需要刷新页面的地方调用函数$scope.reloadRo ...
- maven项目导入war包
导入war包,使用war包中的类和静态资源(css,js,包含jsp等) A项目是被导入的war工程,B项目是需要A项目的一个工程. 在<build>中 <resources> ...
- 讨论HTTP POST 提交数据的几种方式
转自:http://www.cnblogs.com/softidea/p/5745369.html HTTP/1.1 协议规定的 HTTP 请求方法有 OPTIONS.GET.HEAD.POST.PU ...
- 【算法】狄克斯特拉算法(Dijkstra’s algorithm)
狄克斯特拉算法(Dijkstra’s algorithm) 找出最快的路径使用算法——狄克斯特拉算法(Dijkstra’s algorithm). 使用狄克斯特拉算法 步骤 (1) 找出最便宜的节点, ...
- 彻底搞透OAuth 2.0
OAuth是一个关于授权(authorization)的开放网络标准,在全世界得到广泛应用,目前的版本是2.0版. 本文对OAuth 2.0的设计思路和运行流程,做一个简明通俗的解释,主要参考材料为R ...