2016-08-22 59 views
0

我正在嘗試註冊我的自定義日期時間活頁夾,以便我可以解析日期時間以自定義格式進入API。在Web Api中使用自定義DateTime活頁夾2

我試圖註冊我的自定義DateTimeModelBinder但它沒有被擊中,並且調試點不會觸發它,也不會觸發。下面是我在全球asax和WebApiConfig中嘗試的不同方式。

定製綁定註冊

protected void Application_Start() 
     { 
      HttpConfiguration config = GlobalConfiguration.Configuration; 
      config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); 
      config.Formatters.JsonFormatter.UseDataContractJsonSerializer = false; 

      config.BindParameter(typeof(DateTime), new DateTimeModelBinder()); 
      config.BindParameter(typeof(DateTime?), new DateTimeModelBinder()); 

      //ModelBinders.Binders.Add(typeof(DateTime), new DateTimeModelBinder()); 

     GlobalConfiguration.Configuration.Services.Add(typeof(ModelBinderProvider), new DateTimeModelBinderProvider()); 


     GlobalConfiguration.Configure(WebApiConfig.Register); 
     AreaRegistration.RegisterAllAreas(); 
     FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); 
     RouteConfig.RegisterRoutes(RouteTable.Routes); 
     BundleConfig.RegisterBundles(BundleTable.Bundles); 

    } 

網絡API行動

[Route("")] 
     public DataActionResponse PostEvent(EventBindingModel model) 
     { 
      return _eventService 
        .Create(model, CompanyId, UserId) 
        .CreateDataActionResponseSuccess(); 
     } 

DateTimeModelBinder.cs

public class DateTimeModelBinder : IModelBinder 
    { 
     public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext) 
     { 
      var dateToParse = bindingContext.ValueProvider.GetValue(bindingContext.ModelName).AttemptedValue; 

      if (string.IsNullOrEmpty(dateToParse)) 
       return false; 

      bindingContext.ModelState.SetModelValue(bindingContext.ModelName, bindingContext.ValueProvider.GetValue(bindingContext.ModelName)); 


      bindingContext.Model = ParseDateTime(dateToParse); 
      if (bindingContext.Model != null) 
       return true; 


      bindingContext.ModelState.AddModelError(
       key: bindingContext.ModelName, 
       errorMessage: $"Invalid date format for {bindingContext.ModelName}:{dateToParse}. Should be in 'MM/dd/yyyy' OR 'MM/dd/yyyyTHH:mm' format."); 
      return false; 
     } 

     public DateTime? ParseDateTime(string dateToParse) 
     { 
      var enUs = new CultureInfo("en-US"); 

      foreach (var format in SellutionConstants.Globals.BindingDateTimeFormat) 
      { 
       DateTime validDate; 

       if (DateTime.TryParseExact(
        s: dateToParse, 
        format: format, 
        provider: enUs, 
        style: DateTimeStyles.AdjustToUniversal | DateTimeStyles.AllowWhiteSpaces, 
        result: out validDate)) 
       { 
        return validDate; 
       } 
      } 

      return null; 
     } 

    } 

EventBindingModel.cs

public class EventBindingModel 
    { 
     public int? EventId { get; set; } 
     public DateTime StartDate { get; set; } 
     public DateTime EndDate { get; set; } 
    } 

回答

1

嘗試使用SimpleModelBinderProvider

var provider = new SimpleModelBinderProvider(typeof(DateTime), new DateTimeModelBinder()); 
config.Services.Insert(typeof(ModelBinderProvider), 0, provider); 
相關問題