4

I have created a simple custom JsonConverter - code below - to simply change the error message generated when an invalid date is passed.

This is being used in an ASP.Net MVC site and also ASP.Net Web API for Ajax calls.

I am posting a DTO (partial code below) via ajax. When i do this on my local environment everything functions as it should; when the date is invalid I receive a bad request along with the correct error message

When i publish this to a production server it's totally ignored and i receive the default ModelState list of errors.

I have verified my web.config and Newtonsoft versions; they're the same both locally and on the server.

Why would the converter be ignored on production?

Converter is:

public class CustomDateValidationConverter : JsonConverter
{
    public override bool CanConvert(Type objectType)
    {
        return true;
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        if (string.IsNullOrEmpty(reader.Value.ToString()))
            return null;

        DateTime d;
        DateTime.TryParse(reader.Value.ToString(), out d);
        if (d == null || d == DateTime.MinValue)
            throw new Exception(string.Format("{0} is invalid", reader.Path.AddSpacesBeforeUppercase(true)));

        return d;
    }

    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        serializer.Serialize(writer, DateTime.Parse(value as string));
    }
}

I am using it in my DTO as:

.....
    [JsonConverter(typeof(CustomDateValidationConverter))]
    [DateLessThanOrEqual("CreateDateTo", "Create Date From must be on or before Create Date To")]
    public DateTime? CreateDateFrom { get; set; }
.....
Darren Wainwright
  • 30,247
  • 21
  • 76
  • 127
  • I don't think that all versions of asp.net-mvc use Json.NET for model binding. See for instance [How do I sub in JSON.NET as model binder for ASP.NET MVC controllers?](http://stackoverflow.com/questions/20980241/how-do-i-sub-in-json-net-as-model-binder-for-asp-net-mvc-controllers). – dbc Aug 16 '16 at 20:38
  • My deployment contains the .net libraries; AFAIK you don't need to install 'MVC' specifically, on the server – Darren Wainwright Aug 18 '16 at 12:07

1 Answers1

0

Check Your Production Server Date format, It may differ from your Development environment. so It may accept you input date format as valid