muppet
muppet

Reputation: 11

NodaTime types not model binding

For some reason, I can't seem to get request data to bind to a method's NodaTime parameters. With the JsonSerializerSettings configured for NodaTime, I've had no problem serializing NodaTime types and deserializing to them as well. I've tried using the following within Global.asax.cs

JsonConvert.DefaultSettings = () => new JsonSerializerSettings().ConfigureForNodaTime(DateTimeZoneProviders.Tzdb);
GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ConfigureForNodaTime(DateTimeZoneProviders.Tzdb);
// or this
// GlobalConfiguration.Configuration.Formatters.OfType<System.Net.Http.Formatting.JsonMediaTypeFormatter>().First().SerializerSettings.ConfigureForNodaTime(DateTimeZoneProviders.Tzdb);

Example request: /Home/NodaTimeTest?offset=-07

Example endpoint: public object NodaTimeTest(Offset offset)

I've tried other NodaTime types also.

I've read of TypeConverters implemented within NodaTime, which should make custom ModelBinders unnecessary. And so it seems something like this shouldn't be needed:

config.BindParameter(typeof(Offset), new OffsetModelBinder())

What am I doing wrong?

Upvotes: 1

Views: 559

Answers (1)

Buvy
Buvy

Reputation: 1284

The problem was the lack of TypeConverter implementation in NodaTime. This has been fixed with the advent of PR 1237 the converters are now mostly in place. Unfortunately I don't think it will get released into the wild until NodaTime 3.0 however the directions on moving them into your own repository and binding them at runtime is contained in the PR

Upvotes: 1

Related Questions