예제 #1
0
        public void InvokeAction(ControllerContext context, String actionName)
        {
            Type       type       = context.Controller.GetType();
            MethodInfo methodinfo = ApiControllerActionCache.GetMethodInfo(type, actionName);

            if (methodinfo == null)
            {
                throw new Exception("找不到对应webapi的路径");
            }
            Func <IApiController, Object[], Object> func = ApiControllerActionCache.GetMethodFunc(methodinfo);

            ParameterInfo[] parameters  = methodinfo.GetParameters();
            List <Object>   paramsValue = new List <Object>();

            foreach (var param in parameters)
            {
                paramsValue.Add(m_modelBinder.BindModel(context, param.Name, param.ParameterType));
            }
            Object result = func.Invoke(context.Controller, paramsValue.ToArray());

            if (typeof(ActionResult).IsAssignableFrom(result.GetType()))
            {
                m_actionResult = (ActionResult)result;
            }
            else
            {
                m_actionResult      = new DefaultActionResult();
                m_actionResult.Data = result;
            }
            m_actionResult.ExecuteResult(context);
        }
예제 #2
0
        /// <summary>
        /// 这里对类型的名称进行解析,
        /// </summary>
        /// <param name="controllerName"></param>
        /// <returns></returns>
        public IApiController CreateController(String controllerName)
        {
            Type controller = ApiControllerActionCache.GetController(controllerName);

            if (controller == null)
            {
                controller = ApiControllerActionCache.GetController(controllerName + "controller");
            }
            if (controller == null)
            {
                return(null);
            }

            return((IApiController)Activator.CreateInstance(controller));
        }