简体   繁体   中英

Register a custom model binder in MVC 4

In MVC 3, I created custom model binders by creating a new class that implemented Systen.Web.Mvc.IModelBinder , and then registered it inside Global.asax.cs and inside Application_Start() using ModelBinders.Binders.Add(typeof(sometype), new MyModelBinder());

In MVC 4 I understand that you are supposed to use System.Web.Http.ModelBinding.IModelBinder to implement model binders.

So I have two questions:

  1. Why are we supposed to use the model binder from System.Web.Http.ModelBinding instead of the one from Systen.Web.Mvc ? I don't see it is listed as deprecated anywhere, so what is the problem with the old one?

  2. If I implement my model binder from the System.Web.Http.ModelBinding.IModelBinder , how do I register it inside my application so it will actually be used?

You can register your model binder in the same way(in the Global.asax.cs) and you can also use the System.Web.Mvc.IModelBinder interface.

However, the System.Web.Http namespace is basically for the ASP.NET MVC Web API(ie for building web services).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM