我有一些格式奇怪的日期(dd.MM.YYYY)。这是企业希望它展示的方式。所以,我需要一个自定义模型活页夹来读取这个日期。我有这个解决方案工作正常,但是当我切换到使用 OWIN 时,它就停止了工作。这是我现在的代码:
我的 OWIN 启动文件:
公共无效配置(IAppBuilder 应用程序)
{
var config = new HttpConfiguration();
config.BindParameter(typeof(DateTime?), new DateTimeWebApiBinder());
config.BindParameter(typeof(DateTime), new DateTimeWebApiBinder());
app.UseWebApi(config);
}
自定义模型绑定器
公共类 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))
{
返回假;
}
日期时间解析日期;
if (DateTime.TryParseExact(incomingDate.AttemptedValue, "dd.MM.yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out parsedDate))
{
bindingContext.Model = parsedDate;
返回真;
}
返回假;
}
}
发生的事情是自定义模型绑定器没有触发。我怀疑它与 OWIN 设置有关。