EasyUI[转]自定义ASP.NET MVC JsonResult连串化结果

正文转自:http://blog.163.com/luckcq@yeah/blog/static/17174770720121293437119/

 

近来项目中前台页面使用EasyUI的jQuery插件开发中境遇,EasyUI
Form中的Datebox组件绑定ASP.NET
MVC重返的Date提姆e类型的多少失实,因为ASP.NET
MVC重回的Date提姆e类型的JsonResult的结果中的值是“\/Date(277630788015)\/”,于是EasyUI突显的就是回到的值,没有将日期转换,直接呈现在DateBox组件中,解决这一个题材其实有三种艺术:

  1. 增加EasyUI的datebox组件的parser函数自定义格式化日期格式,不过存在一个题材是只要采取form.load数据是先将值赋给datebox不会调用datebox的parser方法,唯有在加载完form后再转移datebox的值为”2011-11-3”格式的值;
  2. 其次种艺术就是本文要讲得修改ASP.NET
    MVC的Json系列化方法,也就是修改JsonResult的种类化方法,上边就来详细说下那种格局。

 

先是看下ASP.NET MVC中的Controller的 Json方法的源码:

        protected internal JsonResult Json(object data) {

            return Json(data, null /* contentType */);

        }

 

        protected internal JsonResult Json(object data, string
contentType) {

            return Json(data, contentType, null /* contentEncoding
*/);

        }

 

        protected internal virtual JsonResult Json(object data, string
contentType, Encoding contentEncoding) {

            return new JsonResult {

                Data = data,

                ContentType = contentType,

                ContentEncoding = contentEncoding

            };

        }

可以看来关键还是在JsonResult那个结果类中,JsonResult类的源码如下:

 

   
[AspNetHostingPermission(System.Security.Permissions.SecurityAction.LinkDemand,
Level = AspNetHostingPermissionLevel.Minimal)]

   
[AspNetHostingPermission(System.Security.Permissions.SecurityAction.InheritanceDemand,
Level = AspNetHostingPermissionLevel.Minimal)]

    public class JsonResult : ActionResult {

 

        public Encoding ContentEncoding {

            get;

            set;

        }

 

        public string ContentType {

            get;

            set;

        }

 

        public object Data {

            get;

            set;

        }

 

        public override void ExecuteResult(ControllerContext context) {

            if (context == null) {

                throw new ArgumentNullException(“context”);

            }

 

            HttpResponseBase response = context.HttpContext.Response;

 

            if (!String.IsNullOrEmpty(ContentType)) {

                response.ContentType = ContentType;

            }

            else {

                response.ContentType = “application/json”;

            }

            if (ContentEncoding != null) {

                response.ContentEncoding = ContentEncoding;

            }

            if (Data != null) {

#pragma warning disable 0618

                JavaScriptSerializer serializer = new
JavaScriptSerializer();

                response.Write(serializer.Serialize(Data));

#pragma warning restore 0618

            }

        }

    }

}

总的来看那里我们应该肯定大家的改动目的了,对,就是ExecuteResult这一个主意,那些主意是系列化Data对象为Json格式的,可知ASP.NET
MVC 使用的是System.Web.Script.Serialization.JavaScriptSerializer类

既然明确了对象,那么就起来出手吧。

1.
扩大JsonResult类自定义个CustomJsonResult类,重写ExecuteResult方法代码如下:

    public class CustomJsonResult:JsonResult

    {

        public override void ExecuteResult(ControllerContext context)

        {

            if (context == null)

            {

                throw new ArgumentNullException(“context”);

            }

 

            HttpResponseBase response = context.HttpContext.Response;

 

            if (!String.IsNullOrEmpty(ContentType))

            {

                response.ContentType = ContentType;

            }

            else

            {

                response.ContentType = “application/json”;

            }

            if (ContentEncoding != null)

            {

                response.ContentEncoding = ContentEncoding;

            }

            if (Data != null)

            {

#pragma warning disable 0618

              

                response.Write(JsonConvert.SerializeObject(Data));

#pragma warning restore 0618

            }

        }

我们选取的是牛顿soft.Json.JsonConvert类连串化对象为Json的,具体集中.NET中的连串化相比可以参考小说:在.NET应用JSON作为数据互换格式

 

  1. 增加Controller重写Json方法,代码如下:

    public class BaseController:Controller

    {

        protected override JsonResult Json(object data, string
contentType, Encoding contentEncoding)

        {

            return new CustomJsonResult

            {

                Data = data,

                ContentType = contentType,

                ContentEncoding = contentEncoding

            };

        }

}

上边就是我们实际使用方法了,因为牛顿soft.Json.JsonConvert类Date提姆e类型可以指定种类化日期的花色为:
[JsonConverter(typeof(IsoDateTimeConverter))],
[JsonConverter(typeof(JavaScriptDateTimeConverter))]

[JsonConverter(typeof(IsoDateTimeConverter))]种类化后的格式为:1981-03-16T00:20:12.1875+08:00

[JsonConverter(typeof(JavaScriptDateTimeConverter))]体系化后的格式为:new Date(-277630787812)

于是我们指定实体类的Date提姆e属性为IsoDate提姆eConverter,代码如下:

 

       [Field(“P_Date”, “更新日期”)]

       [JsonConverter(typeof(IsoDateTimeConverter))]

       public DateTime P_Date { get; set; }

控制器继承自BaseController,Action的回来结果要么JsonResult格式,代码如下:

    public class GoodsController:BaseController

{

        public JsonResult List(string page, string rows)

        {

            Page thepage = new Page() { PageSize = 20, CurrentPage = 1
};

            if (!String.IsNullOrEmpty(rows))

            {

                thepage.PageSize = Convert.ToInt32(rows);

            }

 

            if (!String.IsNullOrEmpty(page))

            {

                thepage.CurrentPage = Convert.ToInt32(page);

            }

            Dictionary<string, object> result = new
Dictionary<string, object>();

            result.Add(“rows”, new
BusinessLogic().SelectByPage<GoodsList>(ref thepage));

            result.Add(“total”, thepage.SumCount);

 

            return Json(result);

        }

}