zoukankan      html  css  js  c++  java
  • MVC中的默认Model绑定者DefaultModelBinder

    一、题外话

    接续上一篇:

    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

  • 相关阅读:
    自己修改的两个js文件
    .net4缓存笔记
    使用.net的Cache框架快速实现Cache操作
    关于招聘面试(转)
    PHP中获取当前页面的完整URL
    Linux在本地使用yum安装软件(转)
    Phalcon的学习篇-phalcon和devtools的安装和设置
    GY的实验室
    aip接口中对url参数md5加密防篡改的原理
    nginx 多站点配置方法集合(转)
  • 原文地址:https://www.cnblogs.com/humble/p/3809696.html
Copyright © 2011-2022 走看看