.NET Model Binders

被刻印的时光 ゝ 提交于 2019-12-09 14:58:11

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/24172263/net-model-binders

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!