Problem with the response in an ASP.NET Web API

3

I have an ASP.NET Web API that is working correctly on other servers, I'm just being presented with a detail on a particular server and the problem has to do with the dates.

I have a class to perform the conversion of the dates and it is:

public class DateTimeConverter : DateTimeConverterBase
   {
      private const string FormatDateTime = "MM/dd/yyyy HH:mm";

      public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
      {
         writer.WriteValue(((DateTime)value).ToString(FormatDateTime));
      }

      public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
      {
         var value = reader.Value?.ToString();
         try
         {
            if (string.IsNullOrEmpty(value) && Nullable.GetUnderlyingType(objectType) != null)
            {
               return null;
            }

            return DateTime.Parse(value);
         }
         catch
         {
            throw new ArgumentNullException($"Unexpected token parsing date. Expected String, got {reader.TokenType}.");
         }
      }
   }

When I make the request to the Web API, the answer I get is this:

[
    {
        "fileName": "VP_1_preview.jpg",
        "lastWriteTime": "06/mm/01/mm/2018 14:06"
    },
    {
        "fileName": "VP_2_preview.jpg",
        "lastWriteTime": "06/mm/01/mm/2018 14:05"
    },
    {
        "fileName": "VP_3_preview.jpg",
        "lastWriteTime": "06/mm/01/mm/2018 14:05"
    },
    {
        "fileName": "VP_4_preview.jpg",
        "lastWriteTime": "06/mm/01/mm/2018 14:04"
    }
]

Where you can see that the lastWriteTime field is adding the characters mm .

In the Web.config of the Web API I also have the following configured:

   <system.web>
      <globalization culture="en-US" uiCulture="en-US" />
      <compilation debug="true" targetFramework="4.5.1" />
      <httpRuntime targetFramework="4.5.1" />
   </system.web>

In the WebConfigApi.cs file I have the following configuration:

   public static class WebApiConfig
   {
      public static void Register(HttpConfiguration config)
      {
         // Enable cors
         config.EnableCors();

         // Web API routes
         config.MapHttpAttributeRoutes();

         config.Routes.MapHttpRoute(
             name: "DefaultApi",
             routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional });

         var jsonFormatter = config.Formatters.JsonFormatter;
         jsonFormatter.SerializerSettings.DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Utc;
         jsonFormatter.SerializerSettings.DateFormatHandling = Newtonsoft.Json.DateFormatHandling.MicrosoftDateFormat;
         jsonFormatter.SerializerSettings.Formatting = Newtonsoft.Json.Formatting.Indented;
         jsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
         jsonFormatter.SerializerSettings.Culture = new CultureInfo("en-US")
         {
            NumberFormat = new NumberFormatInfo()
            {
               NumberDecimalDigits = 2,
               NumberDecimalSeparator = "."
            }
         };
         jsonFormatter.SerializerSettings.Converters.Add(new DateTimeConverter());

         config.Formatters.XmlFormatter.UseXmlSerializer = true;
      }
   }

Also mention that the server has the following configured in the Time Zone option: (UTC-06: 00) Guadalajara, Mexico City, Monterrey

Thank you very much in advance for your comments and valuable help.

    
asked by ericardezp 14.06.2018 в 20:32
source

0 answers