Jsonresult cite a blog

Source: Internet
Author: User
Tags httpcontext

Http://www.cnblogs.com/JerryWang1991/archive/2013/03/08/2950457.html

Recently began to use MVC project, when using Jsonresult return data, the date is reflected into the format of/date 1233455, the Web is the client using JS to deal with this problem, so that you need to do in each of the date where the conversion, To be used.

So I found out by deserializing the Controller abstract class and the Jsonresult class:

Methods for handling objects to JSON strings in Jsonresult:

    PublicOverridevoidExecuteresult (ControllerContext context) {if (context = =Null) {ThrowNew ArgumentNullException ("Context"); }if ((This. Jsonrequestbehavior = = jsonrequestbehavior.denyget) &&String. Equals (context. HttpContext.Request.HttpMethod,"GET", StringComparison.OrdinalIgnoreCase)) {ThrowNewInvalidOperationException (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! = nullthisif (this. Data! = null) {JavaScriptSerializer serializer = new JavaScriptSerializer (); response. Write (serializer. Serialize (this 

In this method, JavaScriptSerializer is used to serialize the object to JSON.

So I customized a class that inherits the Jsonresult class

And the above method from writing, replace with Newton.json JavaScriptSerializer

The replacement code is as follows:

if (null)        {            new"yyyy-mm-dd HH:mm:ss"; Response. Write (Jsonconvert.serializeobject (this. Data, Newtonsoft.Json.Formatting.Indented, TimeFormat, x);); }

This processes the JSON serialization of the date.

Then redefine a controller, named Jsoncontroller, and set to abstract class.

Then override the method:

Json (Object data, String ContentType, Encoding contentencoding, jsonrequestbehavior behavior) {    new Jsonresult {data = data, ContentType = ContentType, contentencoding = contentencoding, Jsonrequestbehav IOR = behavior};}

Change the Jsonresult to we inherit Jsonresult and write the Executeresult method class, the working Gaocheng!

Later as long as the jsonresult involves the return date, you can let the controller inherit our custom class, thereby resolving the date problem.

The above only resolves the date JSON after using the display in JS, not solve the problem of the date involved in the operation, but according to my current experience, JS in the date to participate in the budget time is relatively small, so can solve the problem for display, it is OK,

There are a lot of interesting features in ASP, let's explore together!

Jsonresult cite a blog

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.