사용자 정의 IModelBinder를 사용하여 문자열을 NodaTime LocalDates로 변환하려고합니다. 내 LocalDateBinder
은 다음과 같습니다 : 내 WebApiConfig에서 웹 API IModelBinder를 모든 유형의 인스턴스에 적용하십시오.
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;
}
}
내가
SimpleModelBinderProvider
를 사용하여이 ModelBinder를, 라
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
}
나는 이것이 내가 웹 API와 모델 바인더를 등록하고있어 어떻게 함께 할 수있는 뭔가가 그림,하지만 난 수정해야 내가이에 관해서 손실에 있어요 - 나는 정의를 필요합니까 바인더 공급자?
누구든지이 문제를 해결하기 위해이 문제를 해결하지 못했습니다. 실제 문제는 내 JSON 직렬화 설정이 NodaTime 객체를 비 직렬화하는 방법을 얻고 있다는 것이 었습니다. 기본 DateTime 핸들러를 재정의해야했습니다. –