Sunday, May 1, 2016

Json Data format issue /Date() in MVC

I struggled bit with getting over to this Json date format issue. I got the lead with Hanselman . Scott tried his best to point to right direction but all he was intended towards web api 2. I was having a issue with mvc underlying

After working on this for an hour I started getting lot of solution via stackoverflow and many other blog post.

We can handle this date issue at client side but I personally want to handle everything at server side.

I liked Bipin Joshi Solution, which is kind of straight forward or straight cut solution.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using Newtonsoft.Json;
using System.Xml.Linq;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;

namespace JsonNetExtension
    public class JsonNetResult: JsonResult
        public new object Data { get; set; }

        public override void ExecuteResult(ControllerContext context)
            HttpResponseBase response = context.HttpContext.Response;
            response.ContentType = "application/json";
            if (ContentEncoding != null)
                response.ContentEncoding = ContentEncoding;
            if (Data != null)
                JsonTextWriter writer = new JsonTextWriter(response.Output) { Formatting = Newtonsoft.Json.Formatting.Indented };
                JsonSerializer serializer = JsonSerializer.Create(new JsonSerializerSettings());
                serializer.Serialize(writer, Data);

Post a Comment