Sunday, May 1, 2016

Json Data format issue /Date() in Asp.net 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 asp.net mvc underlying Json.net.


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.




http://www.developer.com/net/dealing-with-json-dates-in-asp.net-mvc.html

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);
                writer.Flush();
            }
        }
    }
}

No comments :