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
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();
            }
        }
    }
}
References
http://stackoverflow.com/questions/10036445/how-does-one-correctly-implement-a-mediatypeformatter-to-handle-requests-of-type
https://blogs.msdn.microsoft.com/henrikn/2012/02/17/using-json-net-with-asp-net-web-api/
http://www.hanselman.com/blog/OnTheNightmareThatIsJSONDatesPlusJSONNETAndASPNETWebAPI.aspx
http://byterot.blogspot.com.au/2012/04/aspnet-web-api-series-part-5.html
http://www.c-sharpcorner.com/UploadFile/97fc7a/mediatypeformatters-in-webapi/
By James NewtonKing
http://james.newtonking.com/archive/2009/02/20/good-date-times-with-json-net
http://stackoverflow.com/questions/10036445/how-does-one-correctly-implement-a-mediatypeformatter-to-handle-requests-of-type
https://blogs.msdn.microsoft.com/henrikn/2012/02/17/using-json-net-with-asp-net-web-api/
http://www.hanselman.com/blog/OnTheNightmareThatIsJSONDatesPlusJSONNETAndASPNETWebAPI.aspx
http://byterot.blogspot.com.au/2012/04/aspnet-web-api-series-part-5.html
http://www.c-sharpcorner.com/UploadFile/97fc7a/mediatypeformatters-in-webapi/
By James NewtonKing
http://james.newtonking.com/archive/2009/02/20/good-date-times-with-json-net
No comments :
Post a Comment