예제 #1
0
        private Controller GetController(string controllerName)
        {
            if (string.IsNullOrWhiteSpace(controllerName))
            {
                return(null);
            }

            string fullyQualifiedControllerName = string.Format("{0}.{1}.{2}{3}, {0}",
                                                                MvcContext.Get.AssemblyName,
                                                                MvcContext.Get.ControllersFolderName,
                                                                controllerName,
                                                                MvcContext.Get.ControllerSuffix);

            Type       controllerType = Type.GetType(fullyQualifiedControllerName);
            Controller controller     = (Controller)_dependencyContainer.CreateInstance(controllerType);

            return(controller);
        }
예제 #2
0
        public IHttpResponse Handle(IHttpRequest request)
        {
            string controllerName;
            string actionName;

            if (request.Path == "/")
            {
                controllerName = "Home";
                actionName     = "Index";
            }
            else
            {
                string[] pathParts = request.Path.ToLower().Split('/', StringSplitOptions.RemoveEmptyEntries);

                controllerName = pathParts.First().Capitalize();
                actionName     = pathParts.Last().Capitalize();
            }

            string controllerAssemblyQualifiedName = string.Concat(_mvcContext.AssemblyName, ".", _mvcContext.ControllersNamespace, ".", controllerName, _mvcContext.ControllersSuffix, ", ", _mvcContext.AssemblyName);

            Type controllerType = Type.GetType(controllerAssemblyQualifiedName);

            if (controllerType == null)
            {
                return(RenderNotFound(request));
            }

            MethodInfo targetMethod = FindMethod(controllerType, actionName, request.Method);

            if (targetMethod == null)
            {
                return(RenderNotFound(request));
            }
            else
            {
                Controller controller = (Controller)_dependencyContainer.CreateInstance(controllerType);

                controller.ViewEngine = _viewEngine;
                controller.Request    = request;
                controller.MvcContext = _mvcContext;

                if (targetMethod.GetCustomAttributes <AuthorizeAttribute>()
                    .Any(attribute => !attribute.IsAuthorized(controller.Identity)))
                {
                    return(new WebServer.Results.RedirectResult("/"));
                }

                ParameterInfo[] parameters = targetMethod.GetParameters();
                object[]        arguments  = new object[parameters.Length];

                for (int parameterIndex = 0; parameterIndex < parameters.Length; parameterIndex++)
                {
                    ParameterInfo parameter = parameters[parameterIndex];

                    Type parameterType = parameter.ParameterType;

                    if (parameterType.IsPrimitiveOrString())
                    {
                        arguments[parameterIndex] = Convert.ChangeType(request.FormData[parameter.Name], parameterType);
                    }
                    else
                    {
                        arguments[parameterIndex] = InstantiateAndFill(parameterType, request.FormData);
                    }

                    controller.ModelState.IsValid = IsValidModel(arguments[parameterIndex]);
                }

                IActionResult actionResult = (IActionResult)targetMethod.Invoke(controller, arguments);

                string result = actionResult.Invoke();

                switch (actionResult)
                {
                case IRedirectable _:
                    return(new WebServer.Results.RedirectResult(result));

                case IViewable _:
                    return(new HtmlResult(result, HttpStatusCode.OK));
                }
            }

            return(new HttpResponse(HttpStatusCode.NotFound));
        }