zoukankan      html  css  js  c++  java
  • MVC源码分析

    这几天老感觉不对, 总觉得少点什么, 今天才发现, 前面 3 里面, 在获取Action参数信息的时候,  少解析了. 里面还有一个比较重要的东西. 今天看也是一样的.

    在 InvokeAction() 方法里面, 有一句代码:

    IDictionary<string, object> parameterValues = this.GetParameterValues(controllerContext, actionDescriptor);

    这个是用来获取参数的. 那么参数是不是随便获取呢? 在Mvc 里面, 页面向Action 传参的时候, 有没有尝试过传一个数组, 然后接收的时候, 也直接解析成数组呢? 或者接收更复杂的类型呢?

    答案都在这一篇里面了. 先来看源码.

    protected virtual IDictionary<string, object> GetParameterValues(ControllerContext controllerContext,
       ActionDescriptor actionDescriptor) { Dictionary
    <string, object> dictionary = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase); foreach (ParameterDescriptor descriptor in actionDescriptor.GetParameters()) { dictionary[descriptor.ParameterName] = this.GetParameterValue(controllerContext, descriptor); } return dictionary; }

    一、源码解析

    1. actionDescriptor.GetParameters()

    //System.Web.Mvc.ReflectedActionDescriptor
    public override ParameterDescriptor[] GetParameters()
    {
        return ActionDescriptorHelper.GetParameters(this, this.MethodInfo, ref this._parametersCache);
    }

    这里应该是获取所有的参数和其描述信息.

    2. this.GetParameterValue() -- 主要方法

    protected virtual object GetParameterValue(ControllerContext controllerContext,
       ParameterDescriptor parameterDescriptor) { Type parameterType
    = parameterDescriptor.ParameterType;
       //根据参数描述来获取参数的处理接口 IModelBinder modelBinder
    = this.GetModelBinder(parameterDescriptor); IValueProvider valueProvider = controllerContext.Controller.ValueProvider; string str = parameterDescriptor.BindingInfo.Prefix ?? parameterDescriptor.ParameterName;
       //获取参数上面的过滤器, 并在下面放入到参数解析上下文中(ModelBindingContext) Predicate
    <string> propertyFilter = GetPropertyFilter(parameterDescriptor); ModelBindingContext bindingContext = new ModelBindingContext { FallbackToEmptyPrefix = parameterDescriptor.BindingInfo.Prefix == null, ModelMetadata = ModelMetadataProviders.Current.GetMetadataForType(null, parameterType), ModelName = str, ModelState = controllerContext.Controller.ViewData.ModelState, PropertyFilter = propertyFilter, ValueProvider = valueProvider };
       //执行参数的处理程序
    return (modelBinder.BindModel(controllerContext, bindingContext) ?? parameterDescriptor.DefaultValue); }

    2.1 GetModelBinder()方法

    private IModelBinder GetModelBinder(ParameterDescriptor parameterDescriptor)
    {
        return (parameterDescriptor.BindingInfo.Binder ?? 
          this.Binders.GetBinder(parameterDescriptor.ParameterType)); }

    这里是根据参数描述来获取参数的处理接口

    public interface IModelBinder
    {
        // Methods
        object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext);
    }

    2.2 BindModel()方法 - 这里看的是 DefaultModelBinder, 后面会自定义一个

    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)) { if (!bindingContext.FallbackToEmptyPrefix) { return null; } ModelBindingContext context = new ModelBindingContext { ModelMetadata = bindingContext.ModelMetadata, ModelState = bindingContext.ModelState, PropertyFilter = bindingContext.PropertyFilter, ValueProvider = bindingContext.ValueProvider }; bindingContext = context; flag = true; } if (!flag) { bool flag2 = ShouldPerformRequestValidation(controllerContext, bindingContext); bool skipValidation = !flag2; ValueProviderResult valueProviderResult = bindingContext.UnvalidatedValueProvider
            .GetValue(bindingContext.ModelName, skipValidation);
    if (valueProviderResult != null) {
           //为简单对象返回参数值
    return this.BindSimpleModel(controllerContext, bindingContext, valueProviderResult); } } if (!bindingContext.ModelMetadata.IsComplexType) { return null; } return this.BindComplexModel(controllerContext, bindingContext); }

    这里面的内容有点多, 也有点复杂, 其实解析到这里, 差不多已经得到我想要的东西了.

    二、自定义ModelBinder

    1. IModelBinder -- 对于相对比较简单的类型, 可以使用这种方式

    首先新建一个稍微复杂一点的类.

    public enum GenderEnum
    { 
        Female,
        Male,
        Unknow
    }
    
    public class ExtInfo
    {
        public string QQ { get; set; }
        public string Phone { get; set; }
    }
    
    public class User
    {
       //这里的构造函数, 我创建了一个ExtInfo实例, 否则, 一会这个ExtInfo对象就是空的, 影响我的演示
    public User() { Extension = new ExtInfo(); }
    public int Id { get; set; } public string Name { get; set; } public GenderEnum Gender { get; set; } public ExtInfo Extension { get; set; } }

    接下来, 看一下控制器代码

    public class ModelController : Controller
    {
        public ActionResult Get(User user)
        {
            return View(user);
        }
    
        public ActionResult Index([ModelBinder(typeof(ModelIndexBinder))]User user)
        {
            return View(user);
        }
    }

    控制器中需要注意的部分, 我已经标红了. 接下来看我自定义的ModelBinder

    public class ModelIndexBinder : IModelBinder
    {
        public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            var request = controllerContext.HttpContext.Request;
            User user = new User
            {
                Id = Convert.ToInt32(request.Params["Id"]),
                Name = request.Params["Name"].ToString(),
                Gender = (GenderEnum)Convert.ToInt32(request.Params["Gender"]),
                Extension = new ExtInfo
                {
                    QQ = request.Params["QQ"].ToString(),
                    Phone = request.Params["Phone"].ToString()
                }
            };
            return user;
        }
    }

    先看两个方法执行的效果吧.

    Index Get
     

    注 : 这里是可以有别的方式来达到目的的, 只需要修改一下url即可:

    http://localhost:11620/model/Get?id=1&name=haha&gender=0&Extension.qq=123123123&Extension.phone=12312341234

    此处是为了举一个例子, 才这么弄的.

    对于比较简单的, 用IModelBinder挺方便的, 但是对于稍微复杂一点的, 可以实现 DefaultModelBinder 来实现.

    不过这部分实现就不解析了, 我暂时没用过. 

    一般对于Url Get请求, 不会很复杂, 只有在使用Post请求的时候, 会遇到比较复杂的处理方式. 但是对于这种复杂的处理, 还有一种比较常用的方式, 就是采用反序列化的方式, 我常用的是 Newtonsoft.

    参考:

      MVC扩展

      深入Asp.net Mvc

     目录已同步

  • 相关阅读:
    【Nginx学习】安装及常用命令
    【Nginx学习】基础知识
    【Nginx学习】Xshell7连接CentOS7艰难轶事
    【LeetCode刷题】5343. 多次求和构造目标数组:妙解
    【LeetCode刷题】1353. 最多可以参加的会议数目
    【转载】priority_queue用法
    【LeetCode刷题】供暖器:妙解
    【妙解】重复的子字符串
    【转载学习】基金理财学习
    【转载】sync_with_stdio + cin.tie
  • 原文地址:https://www.cnblogs.com/elvinle/p/6307136.html
Copyright © 2011-2022 走看看