2014-11-06 12 views
1

일부 날짜가 이상한 형식 (dd.MM.YYYY)으로 나타납니다. 이것은 사업이 그것을 어떻게 표시하기를 바라는 지와 같은 것입니다. 따라서이 날짜를 읽으려면 사용자 정의 모델 바인더가 필요합니다. 나는이 솔루션을 잘 작동 시켰지만 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 시작 파일 : 여기 내 코드는 바로 지금입니다. 나는 그것이 OWIN 설정과 관련이 있다고 생각한다.

+0

달성하려는 목표에 대한 설명과 예상대로 진행되지 않는 문제에 대한 질문을 시작하십시오. 독자는 코드 검사에 빠지기 전에 읽은 내용에 대한 일부 상황을 원합니다. – Kolban

답변

0

this article에 따르면 [ModelBinder(typeof(DateTimeWebApiBinder))]을 컨트롤러의 매개 변수에 넣어야합니다.