.NET模型绑定 [英] .NET Model Binders

查看:122
本文介绍了.NET模型绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的ASP.NET MVC 4项目中创建自定义模型粘合剂。但我得到套牢IModelBinder iterfaces。还有的 IModelBinder接口VS可以找到。在下面的命名空间。

 使用System.Web.Http.ModelBinding;
使用System.Web.Mvc;
使用System.Web.ModelBinding;
布尔BindModel(HttpActionContext ActionContext中,ModelBindingContext的BindingContext)
对象BindModel(ControllerContext controllerContext,ModelBindingContext的BindingContext);
布尔BindModel(ModelBindingExecutionContext modelBindingExecutionContext,ModelBindingContext的BindingContext);

所有相关类例如 ModelBindingContext ModelMetadata 等也被复制。我和我的朋友无法找出什么是如此可怕code复制的目的。微软为什么开发商没有提取常见的类共享程序?
据我了解一个接口在MVC中,人们使用网页API是什么的第三个版本的目的是什么?此接口独立或相互不知何故互连?


解决方案

我不是MS民歌,但我觉得这里就是答案 -


  1. System.Web.ModelBinding 是ASP.Net WebForms的应用

<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.modelbinding%28v=vs.110%29.aspx\">http://msdn.microsoft.com/en-us/library/system.web.modelbinding(v=vs.110).aspx


  

该System.Web.ModelBinding命名空间提供类,使您
  数据对象绑定到ASP.NET Web窗体服务器控件。



  1. System.Web.Mvc 是ASP.Net MVC

<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.mvc%28v=vs.118%29.aspx\">http://msdn.microsoft.com/en-us/library/system.web.mvc(v=vs.118).aspx


  

在System.Web.Mvc命名空间包含的类和接口
  支持ASP.NET模型视图控制器(MVC)框架,用于创建
  Web应用程序。此命名空间包括重新present类
  控制器,控制器工厂,操作结果,视图,部分
  视图,模型粘合剂,以及更多。



  1. System.Web.Http.ModelBinding 是ASP.Net的Web API

<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.http.modelbinding%28v=vs.118%29.aspx\">http://msdn.microsoft.com/en-us/library/system.web.http.modelbinding(v=vs.118).aspx


  

这个命名空间是Web API 2


在短,所有三个框架是相互独立的。你应该记住,他们是单独部署的框架。因此,有必要让所有的人。如果你不需要一个特定的一个,那么从您引用的程序将其删除。它们不依赖于彼此。他们都是来自他们都用于此目的最适合独立的框架。

这是在智能提示 ReSharper的一样的工具,他们可以列出所有的功能。但你是一个谁应该选择基于您的架构和您的需要是正确的。但通常他们是在.NET框架完整版的所有可用的并且可能服务于同样的工作。

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?

解决方案

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.

这篇关于.NET模型绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆