Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET Model Binders

I was trying to create custom model binder in my ASP.NET MVC 4 project. But i get stuck with IModelBinder iterfaces. There are three IModelBinder interfaces VS can find. In following namespaces.

using System.Web.Http.ModelBinding;
using System.Web.Mvc;
using System.Web.ModelBinding;


bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext)
object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext);
bool BindModel(ModelBindingExecutionContext modelBindingExecutionContext, ModelBindingContext bindingContext);

All related classes like ModelBindingContext, ModelMetadata and others are also duplicated. I and my friend can't find out what is the purpose of such terrible code duplication. Why Microsoft developers didn't extract common classes to shared assembly? As i understand one interface is used in MVC, one in Web Api and what is the purpose of the third version? Are this interfaces independent or somehow interconnected with each other?

like image 742
Mark Lavrynenko Avatar asked Jun 11 '14 20:06

Mark Lavrynenko


People also ask

What is model binder in .NET core?

Model binding allows controller actions to work directly with model types (passed in as method arguments), rather than HTTP requests. Mapping between incoming request data and application models is handled by model binders.

What is model binding in Web API?

Model Binding is the most powerful mechanism in Web API 2. It enables the response to receive data as per requester choice. i.e. it may be from the URL either form of Query String or Route data OR even from Request Body. It's just the requester has to decorate the action method with [FromUri] and [FromBody] as desired.

Does MVC use data binding?

MVC doesn't use data bindings like old web api. You have to use model bindings in a MVC or MVVM approach.


1 Answers

I am not MS folk but I think here is the answer -

  1. System.Web.ModelBinding is for ASP.Net WebForms applications

http://msdn.microsoft.com/en-us/library/system.web.modelbinding(v=vs.110).aspx

The System.Web.ModelBinding namespace provides classes that enable you to bind data objects to ASP.NET Web Forms server controls.

  1. System.Web.Mvc is for ASP.Net MVC

http://msdn.microsoft.com/en-us/library/system.web.mvc(v=vs.118).aspx

The System.Web.Mvc namespace contains classes and interfaces that support the ASP.NET Model View Controller (MVC) framework for creating Web applications. This namespace includes classes that represent controllers, controller factories, action results, views, partial view, model binders, and much more.

  1. System.Web.Http.ModelBinding is for ASP.Net Web API

http://msdn.microsoft.com/en-us/library/system.web.http.modelbinding(v=vs.118).aspx

This namespace is for Web API 2

In short, all three frameworks are independent of each other. And you should remember that, they are separately deployable frameworks. Therefore it is necessary to keep all of them. If you don't need a particular one, then remove it from your referenced assemblies. They are not dependent to each other. They are all from separate frameworks they are all used for the purpose best fits.

It is the feature of the intellisense or resharper like tools that they can list all. But you are the one who should pick the right one based on your framework and your need. But usually they are all available in a .net framework full version and probably serve the same work.

like image 199
brainless coder Avatar answered Sep 26 '22 07:09

brainless coder