2012-03-28 16 views
24

私はMVC 4でカスタムIModelBinderを作成する方法を知っておく必要があり、それが変更されました。MVC 4 ModelBinder

実施されなければならない新たな方法がある:

bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext); 

答えて

27

2つのIModelBinderインタフェースがある:

以前のバージョンと同じであり、変更されていない
  1. System.Web.Mvc.IModelBinder
  2. System.Web.Http.ModelBinding.IModelBinderであり、Web APIおよびApiControllerによって使用されます。したがって、基本的にこのメソッドの内部では、actionContext.ActionArgumentsを対応する値に設定する必要があります。モデルインスタンスを返さなくなりました。
+0

YESSS、感謝ダーリン。 –

+3

カスタムモデルのバインダーも登録する必要があります。 ASP.Net Web APIにはMVC3と同じ方法がありません。 [この記事](http://forums.asp.net/t/1773706.aspx/1)を見て、MVC4 Betaでそれを行う方法を見てください。その答えの下部には、[限り、あなたは明示的に使用して、ModelBinderを把握していますが、私は信じ 'GlobalConfiguration.Configuration.ServiceResolver.GetServices ...' – Steve

24

This linkは、完全な答えを提供します。私は参考のためにここに追加しています。信用はasp.netフォーラムでdravvaに行きます。

まず、IModelBinderから派生したクラスを作成します。 Darinの言うとおり、System.Web.Http.ModelBinding名前空間を使用してください。

public class CustomModelBinder : IModelBinder 
{ 
    public CustomModelBinder() 
    { 
     //Console.WriteLine("In CustomModelBinder ctr"); 
    } 

    public bool BindModel(
     HttpActionContext actionContext, 
     ModelBindingContext bindingContext) 
    { 
     //Console.WriteLine("In BindModel"); 
     bindingContext.Model = new User() { Id = 2, Name = "foo" }; 
     return true; 
    } 
} 

次に、あなたの新しいバインダーのファクトリとして機能するプロバイダを、提供、および他の結合剤は、あなたは、将来的に追加することができます。

public class CustomModelBinderProvider : ModelBinderProvider 
{ 
    CustomModelBinder cmb = new CustomModelBinder(); 
    public CustomModelBinderProvider() 
    { 
     //Console.WriteLine("In CustomModelBinderProvider ctr"); 
    } 

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

     return null; 
    } 
} 

最後に、Global.asax.csに次を含めます(Application_Startなど)。

var configuration = GlobalConfiguration.Configuration; 

IEnumerable<object> modelBinderProviderServices = configuration.ServiceResolver.GetServices(typeof(ModelBinderProvider)); 
List<Object> services = new List<object>(modelBinderProviderServices); 
services.Add(new CustomModelBinderProvider()); 
configuration.ServiceResolver.SetServices(typeof(ModelBinderProvider), services.ToArray()); 

ここで、新しいタイプをアクションメソッドのパラメータとして削除することができます。

public HttpResponseMessage<Contact> Get([ModelBinder(typeof(CustomModelBinderProvider))] User user) 

あるいは

public HttpResponseMessage<Contact> Get(User user) 
+0

とglobal.asax.cs' 'でそれを設定することに注意することは難しいです(typeof(CustomModelBinderProvider))]を使用すると、ModelBinderProviderは必要ありません。 –

3

トッドさんの投稿へのポストRC更新:

あなたのモデルバインダープロバイダーが簡素化されている追加:

var configuration = GlobalConfiguration.Configuration; 

configuration.Services.Add(typeof(ModelBinderProvider), new YourModelBinderProvider()); 
+1

これは私のために働いた。これをグローバルに行う方法はありますか?つまり、デフォルトのモデルバインダーを設定しますか? –

8

追加することも、簡単な方法ModelBinderProviderのないモデルバインダーは次のとおりです。

GlobalConfiguration.Configuration.BindParameter(typeof(User), new CustomModelBinder()); 
+0

これは完全に機能しました!何らかの理由で私はこのページの他の例をMVC4と一緒に使うことができませんでした。 ModelBinderProviderのインターフェイスが変更されたようです。しかし、ModelBinderProviderを削除し、このコードをApplication_Startに追加すると素晴らしい結果が得られました! –

関連する問題