asp.net - 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())
我究竟做错了什么?
解决方案
问题是 NodaTime 中缺少 TypeConverter 实现。随着PR 1237的出现,这个问题已经得到解决,现在大部分转换器已经到位。不幸的是,我认为它不会在 NodaTime 3.0 之前发布,但是关于将它们移动到您自己的存储库并在运行时绑定它们的说明包含在 PR 中
推荐阅读
- firefox - 如何将 127.0.0.1 设置为 Firefox 的代理
- javascript - HTML 在另一个脚本完成执行后加载一个脚本
- ruby-on-rails - Active Storage 中的补丁 Blob 模型
- ansible - 如何在 Ansible 中为 `group_vars` 替换 DEFAULT_HASH_BEHAVIOR?
- report - 如何创建 zkteco 自定义报告
- docker - Azure App Service 无法使用自定义容器启动(尝试配置 SSH 连接)
- javascript - 为什么我无法从任何网络连接到 NodeJS express 服务器?
- mysql - Delphi在一些空闲后失去了mysql连接
- android - 在聚焦模式下无法更改光标位置
- vim - Vim 中的替换和更改之间的区别