4

I am trying to deserialize an object in web api controller. It consist of DateTime. The format of date Time property is "dd-MM-YYYY". I have set format of json serialzer dd-MM-YYYY in global.asax file. Still it gives me error "could not convert string to datetime."

Attaching both files as image

Api Controller

Request

Global.asax

------------------Code in Plain Text-------------------

Global.asax file application start function

 protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            //BundleConfig.RegisterBundles(BundleTable.Bundles);
            AuthConfig.RegisterAuth();
            GlobalConfiguration.Configuration.MessageHandlers.Add(new ApiAuthenticationHandler());
            JobScheduler.Start();
            JsonConvert.DefaultSettings = () => new JsonSerializerSettings
            {
                DateFormatString = "dd-MM-yyyy hh:mm tt",
            };
            var dateTimeConverter = new IsoDateTimeConverter
            {
                DateTimeFormat = "dd-MM-yyyy hh:mm tt"
            };
            GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings
                   .Converters.Add(dateTimeConverter);

        }

API Request

{
    "CustomerViewModel": {

        "DOB": "18-12-2018",

    }
}

API Controller

public Guid CustomerSignup(JObject parametrs)
        {
            dynamic jsonParameters = parametrs;
            CustomerViewModel customer = jsonParameters.CustomerViewModel.ToObject<CustomerViewModel>();
            if (ModelState.IsValid)
            {
                try
                {
                    Guid CurrentApplicationId = new Guid(ConfigurationManager.AppSettings["ApplicationId"].ToString());
                    Logic logic = new Logic(CurrentApplicationId);
                    Guid result = logic.AddCustomers(customer, false);
                    if (result != new Guid())
                    {
                        logic.SendVerifcationEmail(result, customer.UserName, customer.UserAddress.Email, Request.RequestUri.GetLeftPart(UriPartial.Authority));
                        return result;
                    }
                    else
                    {
                        return result;
                    }
                }
                catch (Exception ex)
                {
                    if (ex.GetType().Name == "FaultException")
                    {
                        throw new FaultException(ex.Message);
                    }
                    else
                    {
                        throw new FaultException("Fields are not valid.");
                    }
                }
            }
            else
            {
                throw new FaultException("Fields are not valid.");
            }

        }
Safi Mustafa
  • 515
  • 7
  • 22

0 Answers0