예제 #1
0
        static MethodInfo GetAction(ActionDescriptor actionDescriptor)
        {
            HttpController controller = actionDescriptor.Controller;
            string         actionName = actionDescriptor.ActionName;

            Type controllerType = controller.GetType();
            const BindingFlags controllerActionBindingFlags =
                BindingFlags.Instance | BindingFlags.Public | BindingFlags.IgnoreCase;

            return(controllerType.GetMethod(actionName, controllerActionBindingFlags));
        }
예제 #2
0
        /*
         * Now we will create a framework just like the ASP.NET WebApi. We called
         * it LocalApi. The core part of the Api framework is generating response.
         * And the most important type for generating response is the HttpController.
         * In this practice, we try invoking the specified action of the controller
         * to generate a response.
         *
         * The class to invoke controller action is called a ControllerActionInvoker,
         * and the entry point is called InvokeAction. It accepts an actionDescriptor
         * which contains the instance of the controller (currently we have no idea
         * where it comes from), the name of the action to invoke. For simplicity,
         * we assume that all actions contains no parameter.
         */

        public static HttpResponseMessage InvokeAction(ActionDescriptor actionDescriptor)
        {
            HttpController controller = actionDescriptor.Controller;
            var            actionName = actionDescriptor.ActionName;
            Type           type       = controller.GetType();
            MethodInfo     methodInfo = type
                                        .GetMethod(actionName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase);

            if (methodInfo == null)
            {
                return(new HttpResponseMessage(HttpStatusCode.NotFound));
            }
            try
            {
                return((HttpResponseMessage)methodInfo.Invoke(controller, new object[] {}));
            }
            catch (TargetInvocationException)
            {
                return(new HttpResponseMessage(HttpStatusCode.InternalServerError));
            }
        }