Esempio n. 1
0
        public virtual object Invoke(Type controllerType, MethodInfo action, HttpContext context)
        {
            var controller = (Controller)Activator.CreateInstance(controllerType);

            //var controller = (Controller)ServiceResolver.Current.Resolve(controllerType);
            if (controller == null)
            {
                throw new InvalidOperationException(nameof(controller));
            }

            var descriptor = new ControllerActionDescriptor()
            {
                ActionName     = action.Name,
                ControllerName = controllerType.Name,
                MethodInfo     = action
            };

            var actionContext     = new ActionContext(context, descriptor);
            var controllerContext = new ControllerContext(actionContext);

            controller.ControllerContext = controllerContext;

            try
            {
                // Invoke OnActionExecuting()
                var actionExecutingContext = new ActionExecutingContext(controllerContext, controllerContext);
                var actionExecuting        = InvokeActionExecuting(controller, actionExecutingContext);
                if (actionExecuting != null)
                {
                    return(actionExecuting);
                }

                // Invoke controller action ExecutedResult()
                var args   = _modelMapper.Bind(controllerContext);
                var result = (ActionResult)action.Invoke(controller, args);
                result.ExecuteResult(controllerContext);

                // Invoke OnActionExecuted()
                var actionExecutedContext = new ActionExecutedContext(actionContext, controllerContext);
                var actionExecuted        = InvokeActionExecuted(controller, actionExecutedContext);
                if (actionExecuting != null)
                {
                    return(actionExecuting);
                }

                return(result);
            }
            catch (Exception ex)
            {
                // Invoke OnExecption()
                var exceptionContext = new ExceptionContext(controllerContext, ex);
                var exception        = InvokeException(controller, actionContext, exceptionContext);
                if (exception != null)
                {
                    return(exception);
                }

                return(exception);
            }
        }
Esempio n. 2
0
        public RequestDelegate CreateRequestDelegate(Type controllerType, MethodInfo MethodAction)
        {
            return((context) =>
            {
                var controller = (Controller)Activator.CreateInstance(controllerType);
                if (controller == null)
                {
                    throw new InvalidOperationException(nameof(controller));
                }

                var descriptor = new ControllerActionDescriptor()
                {
                    ActionName = MethodAction.Name,
                    ControllerName = controllerType.Name,
                    MethodInfo = MethodAction
                };

                var actionContext = new ActionContext(context, descriptor);
                var controllerContext = new ControllerContext(actionContext);
                controller.ControllerContext = controllerContext;

                try
                {
                    // Invoke OnActionExecuting()
                    var actionExecutingContext = new ActionExecutingContext(controllerContext, controllerContext);
                    var actionExecuting = InvokeActionExecuting(controller, actionExecutingContext);
                    if (actionExecuting != null)
                    {
                        return;
                    }

                    // Invoke controller action ExecutedResult()
                    var args = _modelMapper.Bind(controllerContext);
                    var result = (ActionResult)MethodAction.Invoke(controller, args);
                    result.ExecuteResult(controllerContext);

                    // Invoke OnActionExecuted()
                    var actionExecutedContext = new ActionExecutedContext(actionContext, controllerContext);
                    var actionExecuted = InvokeActionExecuted(controller, actionExecutedContext);
                    if (actionExecuting != null)
                    {
                        return;
                    }

                    return;
                }
                catch (Exception ex)
                {
                    // Invoke OnExecption()
                    var exceptionContext = new ExceptionContext(controllerContext, ex);
                    var exception = InvokeException(controller, actionContext, exceptionContext);
                    if (exception != null)
                    {
                        return;
                    }

                    return;
                }
            });
        }
Esempio n. 3
0
        public void SimpleArray()
        {
            var request = new HttpRequest("GET", "/", "HTTP/1.1");

            request.Form.Add("Ages[]", "8");
            request.Form.Add("Ages[]", "32");

            var modelMapper = new ModelMapper();
            var actual      = modelMapper.Bind <SimpleArrayViewModel>(request, "");

            Assert.Equal(8, actual.Ages[0]);
            Assert.Equal(32, actual.Ages[1]);
        }
Esempio n. 4
0
        public void SimpleMapping()
        {
            var request = new HttpRequest("GET", "/", "HTTP/1.1");

            request.Form.Add("UserName", "Jonas");
            request.Form.Add("FirstName", "Arne");

            var modelMapper = new ModelMapper();
            var actual      = modelMapper.Bind <UserViewModel>(request, "");

            Assert.Equal("Jonas", actual.UserName);
            Assert.Equal("Arne", actual.FirstName);
        }
Esempio n. 5
0
        public void Nested()
        {
            var request = new HttpRequest("GET", "/", "HTTP/1.1");

            request.Form.Add("Author.UserName", "Jonas");
            request.Form.Add("Author.FirstName", "Arne");
            request.Form.Add("Rating", "22");

            var modelMapper = new ModelMapper();
            var actual      = modelMapper.Bind <RatingViewModel>(request, "");

            Assert.Equal("Jonas", actual.Author.UserName);
            Assert.Equal("Arne", actual.Author.FirstName);
        }
Esempio n. 6
0
        public void Array_Indexed()
        {
            var request = new HttpRequest("GET", "/", "HTTP/1.1");

            request.Form.Add("users[0].FirstName", "Hobbe");
            request.Form.Add("users[0].Age", "32");
            request.Form.Add("users[1].FirstName", "Kalle");
            request.Form.Add("users[2].Age", "10");

            var modelMapper = new ModelMapper();
            var actual      = modelMapper.Bind <UserViewModel[]>(request, "users");

            Assert.Equal("Hobbe", actual[0].FirstName);
            Assert.Equal("Kalle", actual[1].FirstName);
        }
Esempio n. 7
0
        public void Array_AssociativeNumeric()
        {
            var request = new HttpRequest("GET", "/", "HTTP/1.1");

            request.Form.Add("Users['0'].FirstName", "Hobbe");
            request.Form.Add("Users['0'].Age", "32");
            request.Form.Add("Users['1'].FirstName", "Kalle");
            request.Form.Add("Users['1'].Age", "10");

            var modelMapper = new ModelMapper();
            var actual      = modelMapper.Bind <SimpleAssocArrayViewModel>(request, "");

            Assert.Equal("Hobbe", actual.Users["0"].FirstName);
            Assert.Equal("Kalle", actual.Users["1"].FirstName);
        }
Esempio n. 8
0
        public void Array_Associative()
        {
            var request = new HttpRequest("GET", "/", "HTTP/1.1");

            request.Form.Add("Users[Jonas].FirstName", "Hobbe");
            request.Form.Add("Users[Jonas].Age", "32");
            request.Form.Add("Users[Arne].FirstName", "Kalle");
            request.Form.Add("Users[Arne].Age", "10");

            var modelMapper = new ModelMapper();
            var actual      = modelMapper.Bind <Dictionary <string, UserViewModel> >(request, "Users");

            Assert.Equal("Hobbe", actual["Jonas"].FirstName);
            Assert.Equal("Kalle", actual["Arne"].FirstName);
        }
Esempio n. 9
0
        public void ViewModelWithArray_Indexed()
        {
            var request = new HttpRequest();

            request.Form.Add("Users[0].FirstName", "Hobbe");
            request.Form.Add("Users[0].Age", "32");
            request.Form.Add("Users[1].FirstName", "Kalle");
            request.Form.Add("Users[2].Age", "10");

            var modelMapper = new ModelMapper();
            var actual      = modelMapper.Bind <UsersViewModel>(request, "");

            Assert.Equal("Hobbe", actual.Users[0].FirstName);
            Assert.Equal("Kalle", actual.Users[1].FirstName);
        }