asp.net mvc resolves the date format returned by the JSON DataGrid is/date (20130450000365)

Source: Internet
Author: User

The

is actually a JSON format problem and we should format it when we return JSON, and we need to rewrite the system's Jsonresult class

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Text;
Using SYSTEM.WEB.MVC;
Using System.Text.RegularExpressions;
Using System.Web;
Using System.Web.Script.Serialization; Namespace App.common {public class Tojsonresult:jsonresult {const string error = "The request has been blocked because sensitive information is disclosed to a third party Web site, which is used when a GET request. In order to be able to get requests, please set Jsonrequestbehavior allowget.
        ";
            <summary>///formatted string///</summary> public string Formatestr {
            Get
        Set
            override void Executeresult (ControllerContext context) {if (context = null)
            {throw new ArgumentNullException ("context"); } if (Jsonrequestbehavior = = Jsonrequestbehavior.denyget && string.equals (context). HttpContext.Request.HttpMethod, "get", StringComparison.OrdinalIgnoreCase)) {throw new Invali DoperationException (Error); } 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) {JavaScriptSerializer serializer = new JavaScriptSerializer ()
                ; String jsonstring = serializer.
    
    
                Serialize (Data);
    
                String p = @ "\\/date\ ((\d+) \+\d+\) \\/";
    
                String p = @ "\\/date\ (\d+\) \\/";
    
                MatchEvaluator matchevaluator = new MatchEvaluator (convertjsondatetodatestring);
    
                Regex reg = new regex (p); jsonstring = Reg. Replace (jsonstring, MatchevaLuator); Response.
            Write (jsonstring); }///<summary>///converts JSON serialization time from/date (1294499956278+0800) to string///</summa Ry> private String convertjsondatetodatestring (Match m) {string result = string.
    
            Empty;
            String p = @ "\d";
            var CArray = M.value.tochararray ();
    
            StringBuilder sb = new StringBuilder ();
            Regex reg = new regex (p); for (int i = 0; i < carray.length; i++) {if Reg. IsMatch (Carray[i]. ToString ())) {sb.
                Append (Carray[i]); }//Reg.
    
            Replace (M.value;
    
            DateTime dt = new DateTime (1970, 1, 1); DT = dt. Addmilliseconds (Long. Parse (sb.)
    
            ToString ())); DT = dt.
    
            ToLocalTime (); result = dt.
    
            ToString ("Yyyy-mm-dd HH:mm:ss"); return result;
    
        }
    }
} 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.