日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

[转]自定义ASP.NET MVC JsonResult序列化结果

發布時間:2025/3/15 asp.net 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [转]自定义ASP.NET MVC JsonResult序列化结果 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

本文轉自:http://blog.163.com/luckcq@yeah/blog/static/17174770720121293437119/

?

最近項目中前臺頁面使用EasyUI的jQuery插件開發中遇到,EasyUI Form中的Datebox組件綁定ASP.NET MVC返回的DateTime類型的數據錯誤,因為ASP.NET MVC返回的DateTime類型的JsonResult的結果中的值是"\/Date(277630788015)\/",于是EasyUI顯示的就是返回的值,沒有將日期轉換,直接顯示在DateBox組件中,解決這個問題其實有兩種辦法:

  • 擴展EasyUI的datebox組件的parser函數自定義格式化日期格式,不過存在一個問題是如果使用form.load數據是先將值賦給datebox不會調用datebox的parser方法,只有在加載完form后再改變datebox的值為”2011-11-3”格式的值;
  • 第二種方式就是本文要講得修改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

    ??????????? }

    ??????? }

    我們使用的是Newtonsoft.Json.JsonConvert類序列化對象為Json的,具體集中.NET中的序列化對比可以參考文章:在.NET使用JSON作為數據交換格式

    ?

  • 擴展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

    ??????????? };

    ??????? }

    }

    下面就是我們實際使用方法了,因為Newtonsoft.Json.JsonConvert類DateTime類型可以指定序列化日期的類型為: [JsonConverter(typeof(IsoDateTimeConverter))], [JsonConverter(typeof(JavaScriptDateTimeConverter))]

    [JsonConverter(typeof(IsoDateTimeConverter))]序列化后的格式為:1981-03-16T00:20:12.1875+08:00

    [JsonConverter(typeof(JavaScriptDateTimeConverter))]序列化后的格式為:new?Date(-277630787812)

    于是我們指定實體類的DateTime屬性為IsoDateTimeConverter,代碼如下:

    ?

    ?????? [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);

    ??????? }

    }

    轉載于:https://www.cnblogs.com/freeliver54/p/4383676.html

    總結

    以上是生活随笔為你收集整理的[转]自定义ASP.NET MVC JsonResult序列化结果的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。