I want my users to be able to post dates to an asp.net web api controller in uk format, such as 01/12/2012 (1st Dec 2012).
From what i can tell by default, only us format is accepted.
Can i change something somewhere so that UK format is the default? I tried changing the globalization setting in the web.config but this had no effect.
Paul
Well, I also wanted to solve this at a global level ... and tore out lots of hair in the process. It turns out there are no extension points in WebApi where one would hope to intercept the incoming form data and modify them as needed. Wouldn't that be nice. So, lacking just that, I dug as deep as I could into WebApi source code to see what I could come up with. I ended up reusing the class in charge of parsing form data to create the model. I added just a few lines to deal specifically with dates. That class, below, can be added to the configuration like this:
The formatter:
Done this using a custom model binder, which is slightly different to the model binders in MVC3:
And in my Global.asax.cs file, add this line to tell the api to use this model binder for DateTime values:
Here is the method in my api controller:
My LeadsIndexViewModel class had several DateTime properties which were now all valid UK date times.
There's an example of localising the jQuery date picker to en-gb here: http://weblogs.asp.net/hajan/archive/2010/10/05/integration-of-jquery-datepicker-in-asp-net-website-localization-part-3.aspx
Also, I see you tried setting the culture to en-GB, but I don't know if you tried setting the UI Culture as well in Web.Config (and I don't know if this affects the jQuery bundles in MVC or not):
... or if that doesn't work (heh), why not pass the value as a string and then parse it in your api controller: