Exemplo n.º 1
0
        public IHttpResponse Handle(IHttpRequest request)
        {
            IDictionary <string, string> getParams  = request.UrlParameters;
            IDictionary <string, string> postParams = request.FormData;
            string requestMethod = request.Method.ToString().ToUpper();

            string[] controllerParams = request.Path.Split("/").TakeLast(2).ToArray();

            string controllerName = StringUtility.Capitalize(controllerParams[0]) + MvcContext.Get.ControllersSuffix;
            string actionName     = StringUtility.Capitalize(controllerParams[1]);

            Controller controller = this.GetController(controllerName);

            if (controller != null)
            {
                controller.Request = request;
                controller.InitializeController();
            }

            MethodInfo method = this.GetMethod(controller, requestMethod, actionName);

            if (method == null)
            {
                return(new NotFoundResponse());
            }

            try
            {
                IEnumerable <ParameterInfo> parameters
                    = method.GetParameters();

                object[] methodParams = this.AddParameters(parameters, getParams, postParams);

                IHttpResponse response = this.GetResponse(method, controller, methodParams);
                return(response);
            }
            catch (Exception e)
            {
                return(new InternalServerErrorResponse(e));
            }
        }