예제 #1
0
        public static ModuleActionInvokedContext InvokeAction(ControllerContext controllerContext, Site site, string moduleUrl, ModulePosition modulePosition)
        {
            try
            {
                HttpContext context = HttpContext.Current;

                var positionId = modulePosition.PagePositionId;
                var moduleName = modulePosition.ModuleName;

                ModuleContext moduleContext = ModuleContext.Create(moduleName, site, modulePosition);
                ModuleContext.Current = moduleContext;

                if (string.IsNullOrEmpty(moduleUrl))
                {
                    var entry = modulePosition.Entry;
                    if (entry == null)
                    {
                        var moduleSetting = moduleContext.GetModuleSettings();
                        if (moduleSetting != null)
                        {
                            entry = moduleSetting.Entry;
                        }
                    }
                    if (entry != null)
                    {
                        moduleUrl = GetEntryUrl(context, moduleContext, entry);
                        if (!string.IsNullOrEmpty(moduleUrl) && !moduleUrl.StartsWith("~"))
                        {
                            moduleUrl = "~" + moduleUrl;
                        }
                    }
                }
                if (string.IsNullOrEmpty(moduleUrl))
                {
                    moduleUrl = "~/";
                }
                else if (moduleUrl[0] != '~')
                {
                    moduleUrl = "~/" + moduleUrl.TrimStart('/');
                }

                var httpContext = new ModuleHttpContext(context
                                                        , new ModuleHttpRequest(context.Request, moduleUrl, moduleContext, controllerContext), new ModuleHttpResponse(context.Response, moduleContext), moduleContext);

                var routeData = moduleContext.FrontEndContext.RouteTable.GetRouteData(httpContext);

                var requestContext = new ModuleRequestContext(httpContext, routeData, moduleContext)
                {
                    PageControllerContext = controllerContext
                };

                string controllerName = requestContext.RouteData.GetRequiredString("controller");
                string actionName     = requestContext.RouteData.GetRequiredString("action");
                var    controller     = (Controller)ControllerBuilder.Current.GetControllerFactory().CreateController(requestContext, controllerName);
                if (controller == null)
                {
                    throw new Exception(string.Format("The module '{0}' controller for path '{1}' does not found or does not implement IController.", moduleName, moduleUrl));
                }
                //if (!(controller is ModuleControllerBase))
                //{
                //    throw new Exception(string.Format("The controller type '{0}' must be inherited from ModuleControllerBase.", controller.GetType().FullName));
                //}
                //ModuleControllerBase moduleController = (ModuleControllerBase)controller;

                //ControllerContext moduleControllerContext = new ControllerContext(requestContext, moduleController);

                InitializeController(controller, requestContext);

                var result = actionInvoker.InvokeActionWithoutExecuteResult(modulePosition, controller.ControllerContext, actionName);
                if (result == null)
                {
                    HandleUnknownAction(controller, actionName);
                }
                return(result);
            }
            catch
            {
                if (modulePosition.SkipError)
                {
                    return(null);
                }
                else
                {
                    throw;
                }
            }
        }
예제 #2
0
파일: ModuleExecutor.cs 프로젝트: Godoy/CMS
        public static ModuleActionInvokedContext InvokeAction(ControllerContext controllerContext, Site site, string moduleUrl, ModulePosition modulePosition)
        {
            try
            {
                HttpContext context = HttpContext.Current;

                var positionId = modulePosition.PagePositionId;
                var moduleName = modulePosition.ModuleName;

                ModuleContext moduleContext = ModuleContext.Create(moduleName, site, modulePosition);
                ModuleContext.Current = moduleContext;

                if (string.IsNullOrEmpty(moduleUrl))
                {
                    var entry = modulePosition.Entry;
                    if (entry == null)
                    {
                        var moduleSetting = moduleContext.GetModuleSettings();
                        if (moduleSetting != null)
                        {
                            entry = moduleSetting.Entry;
                        }
                    }
                    if (entry != null)
                    {
                        moduleUrl = GetEntryUrl(context, moduleContext, entry);
                        if (!string.IsNullOrEmpty(moduleUrl) && !moduleUrl.StartsWith("~"))
                        {
                            moduleUrl = "~" + moduleUrl;
                        }
                    }
                }
                if (string.IsNullOrEmpty(moduleUrl))
                {
                    moduleUrl = "~/";
                }
                else if (moduleUrl[0] != '~')
                {
                    moduleUrl = "~/" + moduleUrl.TrimStart('/');
                }

                var httpContext = new ModuleHttpContext(context
                  , new ModuleHttpRequest(context.Request, moduleUrl, moduleContext, controllerContext), new ModuleHttpResponse(context.Response, moduleContext), moduleContext);

                var routeData = moduleContext.FrontEndContext.RouteTable.GetRouteData(httpContext);

                var requestContext = new ModuleRequestContext(httpContext, routeData, moduleContext) { PageControllerContext = controllerContext };

                string controllerName = requestContext.RouteData.GetRequiredString("controller");
                string actionName = requestContext.RouteData.GetRequiredString("action");
                var controller = (Controller)ControllerBuilder.Current.GetControllerFactory().CreateController(requestContext, controllerName);
                if (controller == null)
                {
                    throw new Exception(string.Format("The module '{0}' controller for path '{1}' does not found or does not implement IController.", moduleName, moduleUrl));
                }
                //if (!(controller is ModuleControllerBase))
                //{
                //    throw new Exception(string.Format("The controller type '{0}' must be inherited from ModuleControllerBase.", controller.GetType().FullName));
                //}
                //ModuleControllerBase moduleController = (ModuleControllerBase)controller;

                //ControllerContext moduleControllerContext = new ControllerContext(requestContext, moduleController);

                InitializeController(controller, requestContext);

                var result = actionInvoker.InvokeActionWithoutExecuteResult(modulePosition, controller.ControllerContext, actionName);
                if (result == null)
                {
                    HandleUnknownAction(controller, actionName);
                }
                return result;
            }
            catch
            {
                if (modulePosition.SkipError)
                {
                    return null;
                }
                else
                {
                    throw;
                }
            }
        }