MVC中的預設Model綁定者DefaultModelBinder

來源:互聯網
上載者:User

標籤:style   blog   http   color   os   art   

一、題外話

接續上一篇:

MVC中Action參數綁定的過程

 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

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.