<span id="mktg5"></span>

<i id="mktg5"><meter id="mktg5"></meter></i>

        <label id="mktg5"><meter id="mktg5"></meter></label>
        最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
        問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
        當前位置: 首頁 - 科技 - 知識百科 - 正文

        解決Asp.net Mvc返回JsonResult中DateTime類型數據格式問題的方法

        來源:懂視網 責編:小采 時間:2020-11-27 22:37:06
        文檔

        解決Asp.net Mvc返回JsonResult中DateTime類型數據格式問題的方法

        解決Asp.net Mvc返回JsonResult中DateTime類型數據格式問題的方法:問題背景: 在使用asp.net mvc 結合jquery esayui做一個系統,但是在使用使用this.json方法直接返回一個json對象,在列表中顯示時發現datetime類型的數據在轉為字符串是它默認轉為Date(84923838332223)的格式,在經過查資料發現使用前端來解決這個問
        推薦度:
        導讀解決Asp.net Mvc返回JsonResult中DateTime類型數據格式問題的方法:問題背景: 在使用asp.net mvc 結合jquery esayui做一個系統,但是在使用使用this.json方法直接返回一個json對象,在列表中顯示時發現datetime類型的數據在轉為字符串是它默認轉為Date(84923838332223)的格式,在經過查資料發現使用前端來解決這個問

        問題背景:

                   在使用asp.net mvc 結合jquery esayui做一個系統,但是在使用使用this.json方法直接返回一個json對象,在列表中顯示時發現datetime類型的數據在轉為字符串是它默認轉為Date(84923838332223)的格式,在經過查資料發現使用前端來解決這個問題的方法不少,但是我又發現在使用jquery easyui時,加載列表數據又不能對數據進行攔截,進行數據格式轉換之后再加載,后來發現可以通過自定義JsonResult實現,認為這種方法比較可行,就開始研究

        我們先來看看jsonResult的源碼

        public class JsonResult : ActionResult
         {
         public JsonResult()
         {
         this.JsonRequestBehavior = System.Web.Mvc.JsonRequestBehavior.DenyGet;
         }
         
         public override void ExecuteResult(ControllerContext context)
         {
         if (context == null)
         {
         throw new ArgumentNullException("context");
         }
         if ((this.JsonRequestBehavior == System.Web.Mvc.JsonRequestBehavior.DenyGet) && string.Equals(context.HttpContext.Request.HttpMethod, "GET", StringComparison.OrdinalIgnoreCase))
         {
         throw new InvalidOperationException(MvcResources.JsonRequest_GetNotAllowed);
         }
         HttpResponseBase response = context.HttpContext.Response;
         if (!string.IsNullOrEmpty(this.ContentType))
         {
         response.ContentType = this.ContentType;
         }
         else
         {
         response.ContentType = "application/json";
         }
         if (this.ContentEncoding != null)
         {
         response.ContentEncoding = this.ContentEncoding;
         }
         if (this.Data != null)
         {
         JavaScriptSerializer serializer = new JavaScriptSerializer();
         response.Write(serializer.Serialize(this.Data));
         }
         }
         
         public Encoding ContentEncoding { get; set; }
         
         public string ContentType { get; set; }
         
         public object Data { get; set; }
         
         public System.Web.Mvc.JsonRequestBehavior JsonRequestBehavior { get; set; }
         }
        }

        當我看到上面代碼中的紅色部分,我感到有些熟悉,心里比較高興,以前使用過ashx來傳json的都應該用過此方法吧

        原來它也是使用這個方法進行序列化的。我們就可以在這個地方先獲取到json序列化之后的字符串!然后做寫“小動作”,就ok了

        下面我就定義了一個自己的JsonResult了

        /// <summary>
         /// 自定義Json視圖
         /// </summary>
         public class CustomJsonResult:JsonResult
         {
         /// <summary>
         /// 格式化字符串
         /// </summary>
         public string FormateStr
         {
         get;
         set;
         }
        
         /// <summary>
         /// 重寫執行視圖
         /// </summary>
         /// <param name="context">上下文</param>
         public override void ExecuteResult(ControllerContext context)
         {
         if (context == null)
         {
         throw new ArgumentNullException("context");
         }
        
         HttpResponseBase response = context.HttpContext.Response;
        
         if (string.IsNullOrEmpty(this.ContentType))
         {
         response.ContentType = this.ContentType;
         }
         else
         {
         response.ContentType = "application/json";
         }
        
         if (this.ContentEncoding != null)
         {
         response.ContentEncoding = this.ContentEncoding;
         }
        
         if (this.Data != null)
         {
         JavaScriptSerializer jss = new JavaScriptSerializer();
         string jsonString = jss.Serialize(Data);
         string p = @"\\/Date\((\d+)\)\\/";
         MatchEvaluator matchEvaluator = new MatchEvaluator(this.ConvertJsonDateToDateString);
         Regex reg = new Regex(p);
         jsonString = reg.Replace(jsonString, matchEvaluator);
        
         response.Write(jsonString);
         }
         }
        
         /// <summary> 
         /// 將Json序列化的時間由/Date(1294499956278)轉為字符串 .
         /// </summary> 
         /// <param name="m">正則匹配</param>
         /// <returns>格式化后的字符串</returns>
         private string ConvertJsonDateToDateString(Match m)
         {
         string result = string.Empty;
         DateTime dt = new DateTime(1970, 1, 1);
         dt = dt.AddMilliseconds(long.Parse(m.Groups[1].Value));
         dt = dt.ToLocalTime();
         result = dt.ToString(FormateStr);
         return result;
         }
         }
        


        在這里做的“小動作”就是紅色部分,得到字符串以后,通過正則表達式的方式獲得Date(12347838383333)的字符串,然后把它轉換為DateTime類型,最后在轉為我們想要的格式即可,這個格式可以使用FormateStr屬性設置。

        剩下的就是使用我們自己定義的JsonResult來替換asp.net mvc默認的JsonResult的問題了,接著從源碼中找答案,下面是Controller類的部分代碼

        protected internal JsonResult Json(object data)
         {
         return this.Json(data, null, null, JsonRequestBehavior.DenyGet);
         }
         
         protected internal JsonResult Json(object data, string contentType)
         {
         return this.Json(data, contentType, null, JsonRequestBehavior.DenyGet);
         }
         
         protected internal JsonResult Json(object data, JsonRequestBehavior behavior)
         {
         return this.Json(data, null, null, behavior);
         }
         
         protected internal virtual JsonResult Json(object data, string contentType, Encoding contentEncoding)
         {
         return this.Json(data, contentType, contentEncoding, JsonRequestBehavior.DenyGet);
         }
         
         protected internal JsonResult Json(object data, string contentType, JsonRequestBehavior behavior)
         {
         return this.Json(data, contentType, null, behavior);
         }
         
         protected internal virtual JsonResult Json(object data, string contentType, Encoding contentEncoding, JsonRequestBehavior behavior)
         {
         return new JsonResult { Data = data, ContentType = contentType, ContentEncoding = contentEncoding, JsonRequestBehavior = behavior };
         }

        以上是Controller類來實例化JsonResult的所有代碼。我們只需寫一個BaseController類,重寫最后一個方法即可,然后我們自己的Controller在繼承BaseController即可

        下面是BaseController類的部分代碼,我們為方便自己個性化的需要又定義了兩個MyJosn的方法

        /// <summary>
         /// 返回JsonResult
         /// </summary>
         /// <param name="data">數據</param>
         /// <param name="contentType">內容類型</param>
         /// <param name="contentEncoding">內容編碼</param>
         /// <param name="behavior">行為</param>
         /// <returns>JsonReuslt</returns>
         protected override JsonResult Json(object data, string contentType, System.Text.Encoding contentEncoding, JsonRequestBehavior behavior)
         {
         return new CustomJsonResult
         {
         Data = data,
         ContentType = contentType,
         ContentEncoding =contentEncoding,
         JsonRequestBehavior = behavior,
         FormateStr = "yyyy-MM-dd HH:mm:ss"
         };
         }
        
         /// <summary>
         /// 返回JsonResult.24 /// </summary>
         /// <param name="data">數據</param>
         /// <param name="behavior">行為</param>
         /// <param name="format">json中dateTime類型的格式</param>
         /// <returns>Json</returns>
         protected JsonResult MyJson(object data, JsonRequestBehavior behavior,string format)
         {
         return new CustomJsonResult
         {
         Data = data,
         JsonRequestBehavior = behavior,
         FormateStr = format
         };
         }
        
         /// <summary>
         /// 返回JsonResult42 /// </summary>
         /// <param name="data">數據</param>
         /// <param name="format">數據格式</param>
         /// <returns>Json</returns>
         protected JsonResult MyJson(object data, string format)
         {
         return new CustomJsonResult
         {
         Data = data,
         FormateStr = format
         };
         }
        

        最后我們在自己的Controller中調用即可

        public class ProjectMileStoneController : BaseController
         {
         /// <summary>
         /// 首頁視圖
         /// </summary>
         /// <returns>視圖</returns>
         public ActionResult Index()
         {
         return this.View();
         }
        
         #region 項目里程碑查詢
        
         /// <summary>
         /// 根據項目編號獲取項目里程碑
         /// </summary>
         /// <param name="projectId">項目編號</param>
         /// <returns>項目里程碑</returns>
         public JsonResult GetProjectMileStoneByProjectId(int projectId)
         {
         IList<ProjectMileStone> projectMileStones = FacadeContainer.Get<IProjectMileStoneService>().GetProjectMileStonesByProjectId(projectId);
         return this.MyJson(projectMileStones, "yyyy.MM.dd");
         }
        
         #endregion
         }
        
        

        原文地址:http://www.cnblogs.com/JerryWang1991

        聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

        文檔

        解決Asp.net Mvc返回JsonResult中DateTime類型數據格式問題的方法

        解決Asp.net Mvc返回JsonResult中DateTime類型數據格式問題的方法:問題背景: 在使用asp.net mvc 結合jquery esayui做一個系統,但是在使用使用this.json方法直接返回一個json對象,在列表中顯示時發現datetime類型的數據在轉為字符串是它默認轉為Date(84923838332223)的格式,在經過查資料發現使用前端來解決這個問
        推薦度:
        標簽: 問題 json json數據
        • 熱門焦點

        最新推薦

        猜你喜歡

        熱門推薦

        專題
        Top
        主站蜘蛛池模板: 91亚洲性爱在线视频| 一级看片免费视频| 在线看片无码永久免费aⅴ | 一级黄色毛片免费看| 久久水蜜桃亚洲av无码精品麻豆| 免费可以在线看A∨网站| a高清免费毛片久久| 亚洲国产成人精品无码区在线秒播| 全部免费国产潢色一级| 久久精品免费一区二区| 免费的黄色的网站| 亚洲电影唐人社一区二区| 亚洲&#228;v永久无码精品天堂久久 | 亚洲国产二区三区久久| 日本免费人成视频播放| 无码精品国产一区二区三区免费 | 精品亚洲成a人在线观看| 久久久无码精品亚洲日韩蜜桃| 午夜一级免费视频| 久久精品私人影院免费看| 精品亚洲福利一区二区| 亚洲国产精品久久网午夜| 亚洲欧洲国产精品香蕉网| 日韩免费观看视频| 67194成手机免费观看| 成在线人视频免费视频| 18禁亚洲深夜福利人口| 亚洲免费观看在线视频| 国产aⅴ无码专区亚洲av| 亚洲国产成人久久精品99| 少妇高潮太爽了在线观看免费| 日本高清免费观看| 一级毛片视频免费| 噜噜综合亚洲AV中文无码| 亚洲制服丝袜精品久久| 久久精品国产精品亚洲毛片| 国产综合精品久久亚洲| 天堂亚洲免费视频| 日本免费中文字幕在线看| 毛片免费全部免费观看| 黄在线观看www免费看|