首页 > 解决方案 > NodaTime 类型不是模型绑定

问题描述

出于某种原因,我似乎无法获取绑定到方法的 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 类型。

我已经阅读了TypeConverter在 NodaTime 中实现的 s,这应该使自定义 ModelBinders 变得不必要。所以似乎不需要这样的东西:

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

我究竟做错了什么?

标签: asp.netnodatime

解决方案


问题是 NodaTime 中缺少 TypeConverter 实现。随着PR 1237的出现,这个问题已经得到解决,现在大部分转换器已经到位。不幸的是,我认为它不会在 NodaTime 3.0 之前发布,但是关于将它们移动到您自己的存储库并在运行时绑定它们的说明包含在 PR 中


推荐阅读