環境:
Windows 2008, VS 2008 SP1, Asp.Net Mvc RC1
請求過程片段:
在請求的Action被調用之前,ControllerActionInvoker.InvokeAction()方法被調用,在這個方法裡 面,有一個ReflectedActionDescriptor的實例會被構建,這個實例包含Action的描述信息。
接著,ControllerActionInvoker.GetParameterValues()方法被調用,通過傳入的之前創建的 ReflectedActionDescriptor實例,獲取Action參數列表(對應的ParameterDescriptor的實例分別被創建 ),進而遍歷各參數,嘗試獲取參數的值,在遍歷的循環裡面, ControllerActionInvoker.GetParameterValue()方法被調用。
以下就是ControllerActionInvoker.GetParameterValue()方法的源代碼:
protected virtual object GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor) { // collect all of the necessary binding properties Type parameterType = parameterDescriptor.ParameterType; IModelBinder binder = GetModelBinder(parameterDescriptor); IDictionary<string, ValueProviderResult> valueProvider = controllerContext.Controller.ValueProvider; string parameterName = parameterDescriptor.BindingInfo.Prefix ?? parameterDescriptor.ParameterName; Predicate<string> propertyFilter = GetPropertyFilter (parameterDescriptor); // finally, call into the binder ModelBindingContext bindingContext = new ModelBindingContext() { FallbackToEmptyPrefix = (parameterDescriptor.BindingInfo.Prefix == null), // only fall back if prefix not specified ModelName = parameterName, ModelState = controllerContext.Controller.ViewData.ModelState, ModelType = parameterType, PropertyFilter = propertyFilter, ValueProvider = valueProvider }; object result = binder.BindModel(controllerContext, bindingContext); return result; }