我正在使用自定义 IModelBinder 尝试将字符串转换为 NodaTime LocalDates。我的LocalDateBinder
样子是这样的:
public class LocalDateBinder : IModelBinder
{
private readonly LocalDatePattern _localDatePattern = LocalDatePattern.IsoPattern;
public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext)
{
if (bindingContext.ModelType != typeof(LocalDate))
return false;
var val = bindingContext.ValueProvider.GetValue(bindingContext.ModelName);
if (val == null)
return false;
var rawValue = val.RawValue as string;
var result = _localDatePattern.Parse(rawValue);
if (result.Success)
bindingContext.Model = result.Value;
return result.Success;
}
}
在我的 WebApiConfig 中SimpleModelBinderProvider
,我使用 la注册了这个模型绑定器
var provider = new SimpleModelBinderProvider(typeof(LocalDate), new LocalDateBinder());
config.Services.Insert(typeof(ModelBinderProvider), 0, provider);
当我有一个采用 LocalDate 类型参数的操作时,这很有效,但如果我有一个更复杂的操作,它在另一个模型中使用 LocalDate,它永远不会被触发。例如:
[HttpGet]
[Route("validateDates")]
public async Task<IHttpActionResult> ValidateDates(string userName, [FromUri] LocalDate beginDate, [FromUri] LocalDate endDate)
{
//works fine
}
[HttpPost]
[Route("")]
public async Task<IHttpActionResult> Create(CreateRequest createRequest)
{
//doesn't bind LocalDate properties inside createRequest (other properties are bound correctly)
//i.e., createRequest.StartDate isn't bound
}
我认为这与我如何使用 Web API 注册模型绑定器有关,但我不知道我需要更正什么 - 我需要自定义绑定器提供程序吗?