private IController GetController(Command command) ...{ //First try to get the controller type from the cache Type controllerType =null; if (_controllerTypeCache.ContainsKey(command.CommandText)) ...{ controllerType = _controllerTypeCache[command.CommandText]; } else ...{ //Otherwise, try to get the controller from the controller pattern string controllerClass = _settings.ControllerPattern.Replace("[ControllerName]", command.ControllerName); controllerType = BuildManager.GetType(controllerClass, false, false); if (controllerType ==null) ...{ _logger.DebugFormat("Controller class '{0}' does not exist, trying to lookup in config", controllerClass); //Couldn't get the controller type from the controller pattern, so look up in the config XmlElement controllerNode = (XmlElement)_settings.ConfigXml.DocumentElement.SelectSingleNode(String.Format("controllers/controller[@name='{0}']", command.ControllerName.ToLower())); if (controllerNode !=null&& controllerNode.HasAttribute("class")) ...{ controllerClass = controllerNode.GetAttribute("class"); controllerType = BuildManager.GetType(controllerClass, true, false); } } if (controllerType ==null) ...{ _logger.DebugFormat("No controller available for '{0}', so using the default controller", command.CommandText); //No controller found so use the default controller. This means that a command //doesn't need it's own controller if there it just needs to show the default //view. controllerType = BuildManager.GetType(_settings.DefaultController, true, false); } //Cache this for next time _controllerTypeCache.Add(command.CommandText, controllerType); } _logger.DebugFormat("Creating controller: {0}", controllerType.ToString()); return (IController)Activator.CreateInstance(controllerType); //重新构造一个对象 }
...{ Type controllerType = controller.GetType(); MethodInfo method = controllerType.GetMethod(command.ActionName); if (method !=null) ...{ _logger.DebugFormat("Found action method '{0}' in controller: {1}, converting parameters...", command.ActionName, controller.GetType().ToString()); //Make up the list of parameters ParameterInfo[] methodParams = method.GetParameters(); List<object> paramList =new List<object>(); foreach (ParameterInfo param in methodParams) ...{ //Convert request param to correct type object convertedValue =null; if (param.ParameterType ==typeof(HttpPostedFile)) ...{ //Get the posted file from the files collection convertedValue = context.Request.Files[param.Name]; } elseif (param.ParameterType ==typeof(Guid)) ...{ //Get the posted file from the files collection string valueAsString = context.Request[param.Name]; if (valueAsString ==null) ...{ convertedValue = Guid.Empty; } else ...{ convertedValue =new Guid(valueAsString); } } else ...{ //Convert value to correct type string valueAsString = context.Request[param.Name]; if (param.ParameterType.IsArray) ...{ //This is an array if (valueAsString.Length >0) ...{ string[] array = valueAsString.Split(','); convertedValue = ArrayConverter.ConvertStringArray(array, param.ParameterType); } } else ...{ //Normal type (not an array) try ...{ convertedValue = Convert.ChangeType(valueAsString, param.ParameterType); } catch(Exception) ...{ convertedValue =null; } } } if (convertedValue ==null) ...{ //The converted value is null, create a new instance of this type //to ensure we are using the defaults for value types. if (param.ParameterType.IsValueType) ...{ convertedValue = Activator.CreateInstance(param.ParameterType); } } //Add converted value to param list paramList.Add(convertedValue); } //Call the action with parameters method.Invoke(controller, paramList.ToArray()); }
6.
没有定义ViewName的话,则根据配置的替换(viewWithNoActionPattern or viewWithNoActionPattern)规则,获取相应的view
最后根据View的类型,进行执行或者重转
i
f (command.View.ViewType == ViewType.Render) ...{ _logger.DebugFormat("Rendering view: {0}", command.View.ViewPath); context.Server.Execute(command.View.ViewPath); } else ...{ _logger.DebugFormat("Redirecting to url: {0}", command.View.ViewUrl); context.Response.Redirect(command.View.ViewUrl); }