c# - My custom date format is not formatting date with newton soft json in mvc -


i trying display date in dd-mm-yyyy date getting in format:

2016-08-08t16:17:40.643

i using asp.net mvc , returning data in json format displaying date angular js.

here answer trying link , have combined answer given perishable dave , dav_i:

     public class jsonnetfilterattribute : actionfilterattribute         {             public override void onactionexecuted(actionexecutedcontext filtercontext)             {                 if (filtercontext.result jsonresult == false)                 {                     return;                 }                  filtercontext.result = new jsonnetresult(                     (jsonresult)filtercontext.result);             }              private class jsonnetresult : jsonresult             {                 private const string _dateformat = "dd-mm-yyyy";                 public jsonnetresult(jsonresult jsonresult)                 {                      this.contentencoding = jsonresult.contentencoding;                     this.contenttype = jsonresult.contenttype;                     this.data = jsonresult.data;                     this.jsonrequestbehavior = jsonresult.jsonrequestbehavior;                     this.maxjsonlength = jsonresult.maxjsonlength;                     this.recursionlimit = jsonresult.recursionlimit;                 }                  public override void executeresult(controllercontext context)                 {                     if (context == null)                     {                         throw new argumentnullexception("context");                     }                      var ismethodget = string.equals(                         context.httpcontext.request.httpmethod,                         "get",                         stringcomparison.ordinalignorecase);                      if (this.jsonrequestbehavior == jsonrequestbehavior.denyget                         && ismethodget)                     {                         throw new invalidoperationexception(                             "get not allowed! change jsonrequestbehavior allowget.");                     }                      var response = context.httpcontext.response;                      response.contenttype = string.isnullorempty(this.contenttype)                         ? "application/json"                         : this.contenttype;                      if (this.contentencoding != null)                     {                         response.contentencoding = this.contentencoding;                     }                      if (this.data != null)                     {                         // using json.net serializer                         var isoconvert = new isodatetimeconverter();                         isoconvert.datetimeformat = _dateformat;                         response.write(jsonconvert.serializeobject(this.data));                     }                 }             }         }  [jsonnetfilter] public actionresult getjson() {     return json(new { hello = new date(2016-08-02 05:49:11.000) }, jsonrequestbehavior.allowget) } 

how date in dd-mm-yyyy format??

you're not passing isoconvert variable jsonconvert.serializeobject(this.data), it's never used. need pass serializeobject using appropriate overload:

response.write(jsonconvert.serializeobject(this.data, new [] { isoconvert } )); 

Comments

Popular posts from this blog

amazon web services - S3 Pre-signed POST validate file type? -

c# - Check Keyboard Input Winforms -