0

I'm trying to set my WEB API Application to accept post requests that include a DateTime property which is passed through in dd/MM/yyyy format.

In my Startup class, i have configured the request localization as follows :-

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            //app.UseAuthentication();
            var supportedCultures = new[]
            {

                new CultureInfo("en-AU"),
                new CultureInfo("en-GB"),
                new CultureInfo("en"),
                new CultureInfo("es-ES"),
                new CultureInfo("es-MX"),
                new CultureInfo("es"),
                new CultureInfo("fr-FR"),
                new CultureInfo("fr"),
            };

            app.UseRequestLocalization(new RequestLocalizationOptions
            {
                DefaultRequestCulture = new RequestCulture("en-GB"),
                // Formatting numbers, dates, etc.
                SupportedCultures = supportedCultures,
                // UI strings that we have localized.
                SupportedUICultures = supportedCultures
            });

            app.UseMvc();
        }

However, Model binding fails on a test date such as 22/12/1977. For me the middleware is being ignored.

Derek
  • 8,300
  • 12
  • 56
  • 88
  • Web API uses JSON. While there's no standard date format, everyone uses the ISO 8601 format. Why are you trying to change to something guaranteed to confuse every client of the API? It's the *client's* job to send correctly formatted data. – Panagiotis Kanavos Jun 28 '18 at 10:50
  • 1
    If you need a conversion based on a specific culture, why not accept a string and convert manually? – Sefe Jun 28 '18 at 10:51
  • Thats a good point @Sefe – Derek Jun 28 '18 at 10:52
  • BTW if the client is a web page, all that's needed to send the correct format is to call `.toISOString()` on a Date object instead of `toString()` or accepting raw user input – Panagiotis Kanavos Jun 28 '18 at 10:52
  • @Derek not really - the *clients* won't know that you are breaking the convention. If you use MVC in the same project it's trivial to send the correct dates back to the API controllers – Panagiotis Kanavos Jun 28 '18 at 10:52
  • @Derek if you are using eg jQuery DatePicker [this question](https://stackoverflow.com/questions/7500058/how-to-change-date-format-mm-dd-yy-to-yyyy-mm-dd-in-date-picker/37473649) shows how to use the `dateformat` parameter or the `data-date-format` data binding attribute – Panagiotis Kanavos Jun 28 '18 at 10:57

0 Answers0