I'm investigating the use of NodaTime LocalDate to replace our existing use of of the BCL DateTime/DateTimeOffset classes. We have run into a number of timezone related issues with our code due to our misunderstanding of the arguably ambiguous behavior of DateTime.
To fully leverage NodaTime I want to be able to send and receive dates from our ASP.NET Web API 2 web services of the form YYYY-MM-DD. I have had success in properly serializing LocalDate to YYYY-MM-DD. However I am unable to deserialize a date query parameter to a LocalDate. The LocateDate is always 1970-01-01.
Here is my current prototype code (some code removed for clarity):
PeopleController.cs
[RoutePrefix("api")]
public class PeopleController : ApiController
{
[Route("people")]
public LocalDate GetPeopleByBirthday([FromUri]LocalDate birthday)
{
return birthday;
}
}
Global.asax.cs
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
// Web API configuration and services
var formatters = GlobalConfiguration.Configuration.Formatters;
var jsonFormatter = formatters.JsonFormatter;
var settings = jsonFormatter.SerializerSettings;
settings.ConfigureForNodaTime(DateTimeZoneProviders.Tzdb);
settings.Formatting = Formatting.Indented;
settings.ContractResolver = new CamelCasePropertyNamesContractResolver();
GlobalConfiguration.Configure(WebApiConfig.Register);
}
}
I execute the web service via
http://localhost/api/people?birthday=1980-11-20
However, what is returned is January 1, 1970. Stepping into the code I confirm that birthday
is set to 1970-01-01.
How can I configure the serialization such that the date specified in the URL as a query parameter (or path element) can be properly serialized into a NodaTime LocalDate?