由于某些原因,我似乎无法获得绑定到方法的NodaTime参数的请求数据。有了为NodaTime配置的JsonSerializerSettings,序列化NodaTime类型和反序列化它们就没有问题了。我尝试在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);示例请求:/Home/NodaTimeTest?offset=-07
示例端点:public object NodaTimeTest(Offset offset)
我也尝试过其他类型的NodaTime。
我读到过在NodaTime中实现的TypeConverter,这应该会使定制的ModelBinders变得不必要。所以看起来像这样的东西是不需要的:
config.BindParameter(typeof(Offset), new OffsetModelBinder())我做错了什么?
发布于 2018-12-29 06:30:32
问题在于NodaTime中缺少TypeConverter实现。随着PR 1237的出现,这个问题已经得到了解决,现在转换器已经基本到位。不幸的是,我不认为它会在NodaTime 3.0之前发布,但是关于将它们移动到您自己的存储库并在运行时绑定它们的说明包含在PR中
https://stackoverflow.com/questions/51528654
复制相似问题