ASP. NET has no magic-binding and parsing ASP. net mvc model (Part 1), asp. netmvc
The previous article introduced ASP. net mvc model binding and verification functions, in line with ASP. net mvc has no magic spirit. In this chapter, we will discuss ASP from the perspective of code.. net mvc has learned how to bind and verify the model.
The main contents of this article include:
● ModelBinder
● ValuePrivoder
● ModelMetadata
● Simple and complex models
● Summary
ModelBinder
ModelBinder is the core component used by ASP. net mvc for model binding. All ModelBinder implements the IModelBinder interface, for example:
This interface has only one method, that is, model binding is completed based on the controller and the binding context.
There are different ModelBinder in ASP. net mvc, which are used to bind different types of data, such as common. Net objects and files uploaded over HTTP.
By default, the following five types of ModelBinder are available:
● Defamodelmodelbinder: the default model binder. Generally, requests submitted from the browser are bound to the model using the default processor.
● HttpPostedFileBaseModelBinder: HTTP File model binding
● ByteArrayModelBinder: bind binary data.
● LinqBinaryModelBinder: bind the request to the System. Data. Linq. Binary object. Reference: http://stephenwalther.com/archive/2009/02/25/asp-net-mvc-tip-49-use-the-linqbinarymodelbinder-in-your
● CancellationTokenModelBinder: provides a mechanism for canceling model binding.
All ModelBinder are managed by a dictionary named ModelBinderDictionary, which exists in the definition of the Controller type, as shown in. It is a protected internal attribute, used to complete model binding during Controller execution:
ModelBinderDictionary definition:
From the definition of ModelBinderDictionary, we can see that it implements a dictionary interface and a set interface that uses the Type as the Key and the IModelBinder Type as the value. You can dynamically increase or decrease ModelBinder according to the ModelBinder Type, in addition, there is a DefaultBinder, which generally runs the following instance:
The ModelBinder dictionary in Controller contains the five ModelBinder described above.