2012-04-10 34 views
5

所以好像有幾個人(如herehere)與MVC4模型綁定ApiControllers有問題,但他們都沒有解決我看到的問題。如何在MVC4中調用自定義模型聯編程序?

我真的很想做的就是改變整數列表的數組綁定行爲。所以說,我有一個請求類型是這樣的:

public class MyRequestModel 
{ 
    public List<long> ListOfIntegers { get; set; } 

    ... 
} 

而且像這樣的API GET方法:

public ResultsResponseModel Get(MyRequestModel request) 
{ 
    // use request.ListOfIntegers meaningfully 

    ... 

    return response; 
} 

我基本上要能夠說/api/results/?listOfIntegers=1+2+3+4+5和有決心的List<long>財產。

我已經嘗試了我通常的模型綁定技巧,但是與大多數Web API在MVC4中似乎有一個完全獨立的模型綁定路徑。

我已經使用上MyRequestModel一個System.Web.Http.ModelBinding.ModelBinder屬性,並創建一個模型綁定該「實施」 System.Web.Http.ModelBinding.IModelBinder得到最遠。這始終會產生一個對象引用異常,它不會觸及我的代碼。

有人打過嗎?想想接下來要嘗試什麼?

UPDATE:下面是我在我的自定義ExceptionFilterAttribute已經捕捉到了一個堆棧跟蹤:

Object reference not set to an instance of an object. 
    at System.Web.Http.ModelBinding.DefaultActionValueBinder.BindParameterValue(HttpActionContext actionContext, HttpParameterBinding parameterBinding) 
    at System.Web.Http.ModelBinding.DefaultActionValueBinder.<>c__DisplayClass1.BindValuesAsync>b__0(RequestContentReadKind contentReadKind) 
    at System.Threading.Tasks.TaskHelpersExtensions.<>c__DisplayClass38.<ToAsyncVoidTask>b__37() 
    at System.Threading.Tasks.TaskHelpers.RunSynchronously[TResult](Func`1 func, CancellationToken cancellationToken) 
+0

您可以提交堆棧跟蹤嗎? – jorgehmv 2012-04-10 22:03:00

+0

剛剛添加堆棧跟蹤 – 2012-04-10 22:37:38

回答

4

如果你談論ApiControllers,那麼你想綁定的網絡API模型,現在MVC 這裏的一個示例模型粘合劑

public class MyRequestModelBinderProvider : ModelBinderProvider 
    { 
     MyRequestModelBinder binder = new MyRequestModelBinder(); 
     public IdeaModelBinderProvider() 
     {   
     } 

     public override IModelBinder GetBinder(HttpActionContext actionContext, ModelBindingContext bindingContext) 
     { 
      if (bindingContext.ModelType == typeof(MyRequestModel)) 
      { 
       return binder; 
      } 

      return null; 
     } 
    } 

這裏的註冊自定義模型粘合劑提供商

的一個例子210
IEnumerable<object> modelBinderProviderServices = GlobalConfiguration.Configuration.ServiceResolver.GetServices(typeof(ModelBinderProvider)); 
List<Object> services = new List<object>(modelBinderProviderServices); 
services.Add(new MyRequestModelBinderProvider()); 
GlobalConfiguration.Configuration.ServiceResolver.SetServices(typeof(ModelBinderProvider), services.ToArray()); 

現在,在您的自定義模型綁定您使用上下文來訪問查詢字符串值

public class MyRequestModelBinder : IModelBinder 
    { 
     public MyRequestModelBinder() 
     { 

     } 

     public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext) 
     { 
      MyRequestModel yourModel; 
      //use contexts to access query string values 
      //create/update your model properties 

      bindingContext.Model = yourModel; 
      //return true || false if binding is successful 
     } 

確保您使用的類和接口的WebAPI而不是MVC。某些名稱相同,但名稱空間和dll不同

+0

好,所以我認爲我已經有適當的結構。你知道是否有任何方法來調用默認的Web API模型綁定,然後手動控制模型聯編程序中的列表值參數? – 2012-04-11 13:27:25

+0

在您的自定義ModelBinderProvide中調用base.GetBinder以獲取默認模型聯編程序,並將其放入您的自定義模型聯編程序的專用字段中,以便您可以在模型聯編程序中使用它。 – 2012-04-11 20:45:52

+0

@FrancescoAbbruzzese我喜歡做這樣的事情 - 不幸的是,因爲我們從ModelBinderProvider繼承而來,ModelBinderProvider是一個帶有抽象基礎GetBinder方法的抽象類,所以沒有默認的準備去找我。我試過從'System.Web.Http.Modelbinding.ModelBinders'中的類型發送實例化,沒有太多的運氣。任何其他想法? – 2012-04-12 13:46:55

相關問題