Resolving date serialization format problems with a JSON converter that customizes the controller of MVC

Source: Internet
Author: User

Today, when loading data using the Easyui DataGrid in the MVC Framework, the JSON date format returned by the server is/date (1433088000000+0800)/, requires further conversion by the client, and does not conform to the Easyui commonly used date format requirements. , this paper studies the controller under the MVC framework, discovers that the problem can be solved by extending the controller's JSON method, and can satisfy the serialization format requirement of any kind of data by further customizing the serialization class.

To achieve this goal, you need to complete the three-step process:

1. Create a derived class for the controller and introduce the custom Jsonresult

2. Create a derived class of Jsonresult to implement a custom implementation of the JSON date format

3. All controllers that need to implement a custom date serialization format must inherit from the controller's derived class

Examples of specific code implementations are:

<summary>///implementation of custom serialization dates by overloading the Executeresult method//</summary>public class vmejsonresult:jsonresult{P ublic override void Executeresult (ControllerContext context) {if (context = = null) {throw NE        W ArgumentNullException ("context"); } httpresponsebase response = context.        Httpcontext.response; if (this.            Data! = null) {jsonserializersettings setting = new Jsonserializersettings (); Sets the format of the date serialization setting.            dateformatstring = "Yyyy-mm-dd HH:MM:ss"; Response.        Write (Jsonconvert.serializeobject (Data, setting)); }}}///<summary>/////</summary>public class Vmecontroller:cont by creating a derived class of controller to introduce a custom JSON implementation roller{protected override Jsonresult Json (object data, String ContentType, Encoding contentencoding) {retur    N new Vmejsonresult {data = data, ContentType = ContentType, contentencoding = contentencoding}; } Public New JsonresuLt Json (Object data, Jsonrequestbehavior jsonrequest) {return new Vmejsonresult {data = data, Jsonrequestbehav    IOR = jsonrequest}; } Public new Jsonresult Json (object data) {return new Vmejsonresult {data = data, Jsonrequestbehavior = Jso    Nrequestbehavior.allowget}; }}///<summary>///all controllers that need to implement a custom date serialization effect must inherit from the vmecontroller///</summary>public class Couponcontroller:    vmecontroller{public ActionResult Index () {return View ();        } public ActionResult Getallcoupontypes () {Hashtable Hashtable = new Hashtable ();        hashtable["sessionId"] = "";        String json = Jsonhelper.serialize (hashtable);        String Retjson = Httphelper.postforjson ("Http://localhost/vme", "Couponservice.svc", "Getallcoupontypes", JSON);        String jsonresult = Jsonhelper.getstring (Retjson);        list<coupontype> results = jsonhelper.deserializeobject<list<coupontype>> (JsonResult); Return JSON (results, JSONRequestbehavior.allowget); }}




Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Resolving date serialization format problems with a JSON converter that customizes the controller of MVC

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.