MVC中的默认Model绑定者DefaultModelBinder
一、题外话
接续上一篇:
public IModelBinder DefaultBinder { get { if (this._defaultBinder == null) { this._defaultBinder = new DefaultModelBinder(); } return this._defaultBinder; } set { this._defaultBinder = value; } }
二、分析DefaultModelBinder
public class DefaultModelBinder : IModelBinder { public virtual object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext){}//其他方法我们暂时不分心,不止这一个方法 }
public virtual object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { EnsureStackHelper.EnsureStack(); if (bindingContext == null) { throw new ArgumentNullException("bindingContext"); } bool flag = false; if (!string.IsNullOrEmpty(bindingContext.ModelName) && !bindingContext.ValueProvider.ContainsPrefix(bindingContext.ModelName)) {//ValueProvider是一个IValueProvider的实例,我们下面分析一下这个东西 if (!bindingContext.FallbackToEmptyPrefix) { return null; } bindingContext = new ModelBindingContext { ModelMetadata = bindingContext.ModelMetadata, ModelState = bindingContext.ModelState, PropertyFilter = bindingContext.PropertyFilter, ValueProvider = bindingContext.ValueProvider }; flag = true; } if (!flag) { bool flag2 = DefaultModelBinder.ShouldPerformRequestValidation(controllerContext, bindingContext); IUnvalidatedValueProvider arg_9B_0 = bindingContext.UnvalidatedValueProvider; bool skipValidation = !flag2; ValueProviderResult value = arg_9B_0.GetValue(bindingContext.ModelName, skipValidation); if (value != null) { return this.BindSimpleModel(controllerContext, bindingContext, value); } } if (!bindingContext.ModelMetadata.IsComplexType) { return null; } return this.BindComplexModel(controllerContext, bindingContext); }
看看IValueProvider
public interface IValueProvider { /// <summary>Determines whether the collection contains the specified prefix.</summary> /// <returns>true if the collection contains the specified prefix; otherwise, false.</returns> /// <param name="prefix">The prefix to search for.</param> bool ContainsPrefix(string prefix); /// <summary>Retrieves a value object using the specified key.</summary> /// <returns>The value object for the specified key.</returns> /// <param name="key">The key of the value object to retrieve.</param> ValueProviderResult GetValue(string key); }
可以看看这篇博文:http://www.cnblogs.com/artech/archive/2012/05/17/value-provider-01.html
少侠,我看你气度不凡天赋异禀,骨骼精奇,这么帅,来了就帮推荐一把吧
我的最近更新