我的OWIN应用程序不会触发我的自定义模型绑定器
问题描述:
我有一些日期格式奇怪(dd.MM.YYYY)。这是企业想要展示的东西。所以,我需要一个自定义模型联编程序来读取这个日期。我有这个解决方案工作正常,但是当我切换到使用OWIN,然后停止工作。这里是我的代码现在:我的OWIN应用程序不会触发我的自定义模型绑定器
我OWIN启动文件:
public void Configuration(IAppBuilder app) { var config = new HttpConfiguration(); config.BindParameter(typeof(DateTime?), new DateTimeWebApiBinder()); config.BindParameter(typeof(DateTime), new DateTimeWebApiBinder()); app.UseWebApi(config); }
自定义模型绑定
public class DateTimeWebApiBinder : IModelBinder { public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext) { var incomingDate = bindingContext.ValueProvider.GetValue(bindingContext.ModelName); if (bindingContext.ModelType == typeof(DateTime) && string.IsNullOrEmpty(incomingDate.AttemptedValue)) { return false; } DateTime parsedDate; if (DateTime.TryParseExact(incomingDate.AttemptedValue, "dd.MM.yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out parsedDate)) { bindingContext.Model = parsedDate; return true; } return false; } }
这是怎么回事,是自定义模型粘合剂没有触发。我怀疑它与OWIN设置有关。
请考虑开始你的问题,描述你正在努力达到什么样的目标,以及未能达到预期的目标。读者在进入代码审查之前需要了解他们正在阅读的内容。 – Kolban 2014-11-06 19:53:40