文章出處

這幾天老感覺不對, 總覺得少點什么, 今天才發現, 前面 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

 

 目錄已同步


文章列表




Avast logo

Avast 防毒軟體已檢查此封電子郵件的病毒。
www.avast.com


arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()