Inheritance: EntityBase
        public virtual ModuleRequestResult ExecuteModule(HttpContextBase httpContext, Module moduleModel, string moduleRoute)
        {
            Arg.NotNull("httpContext", httpContext);
            Arg.NotNull("moduleModel", moduleModel);
            Arg.NotNull("moduleRoute", moduleRoute); // Empty route is OK!

            // If the module application for this module is installed
            if (ModuleApplications.Contains(moduleModel.ModuleApplicationId)) {
                // Get the module's application
                ModuleApplication app = ModuleApplications[moduleModel.ModuleApplicationId].GetExportedObject();

                // Setup the module's context
                ModuleRequestContext moduleRequestContext = new ModuleRequestContext {
                    Application = app,
                    Module = moduleModel,
                    ModuleRoutingUrl = moduleRoute,
                    HttpContext = httpContext
                };

                // Run the module
                ModuleRequestResult result = app.ExecuteRequest(moduleRequestContext);
                return result;
            }
            return null;
        }
Beispiel #2
0
        public ActionResult ViewPage(int? moduleId, string moduleRoute)
        {
            // Get the current page
            Page page = ActivePage;

            // Start setting up the view model
            PageViewModel pageModel = new PageViewModel {
                Page = page
            };

            IList<Module> modules = page.Modules.ToList();

            // Execute the control panel module if specified and the user has permission
            if (!String.IsNullOrEmpty(ControlPanelId)) {
                Guid controlPanelGuid = Guid.Empty;
                try {
                    controlPanelGuid = new Guid(ControlPanelId);
                } catch(FormatException) {}
                if (controlPanelGuid != Guid.Empty) {
                    Module controlPanelModule = new Module() { ModuleApplicationId = controlPanelGuid };
                    ModuleRequestResult controlPanelResult = ModuleExecutor.ExecuteModule(HttpContext,
                                                                                          controlPanelModule,
                                                                                          String.Empty);

                    // If the result is a "Page Override" result (meaning it should be executed as the sole ActionResult for this page)
                    // Then execute it immediately
                    if (controlPanelResult != null && controlPanelResult.ActionResult is PageOverrideResult) {
                        return new RenderModuleResult { ModuleRequestResult = controlPanelResult };
                    }
                    pageModel.ControlPanelResult = controlPanelResult;
                }
            }

            // If there is a selected module (moduleId != null), run it first
            ModuleRequestResult selectedResult = null;
            if (moduleId.HasValue) {
                Module selectedModule = modules.SingleOrDefault(m => m.Id == moduleId.Value);
                if (selectedModule != null) {
                    selectedResult = ModuleExecutor.ExecuteModule(HttpContext, selectedModule, moduleRoute);

                    // See Control Panel above
                    if (selectedResult != null && selectedResult.ActionResult is PageOverrideResult) {
                        return new RenderModuleResult {ModuleRequestResult = selectedResult};
                    }
                }
            }

            // Execute all the modules on the page
            foreach (Module module in page.Modules) {
                ModuleRequestResult result;

                // If the current module is the selected module, then we already have the result, so we
                // don't need to execute it.
                if (moduleId.HasValue && moduleId.Value == module.Id) {
                    result = selectedResult;
                } else {
                    result = ModuleExecutor.ExecuteModule(HttpContext, module, String.Empty);
                }

                if (result != null) {
                    // Store the result, only the selected module can override the page
                    pageModel[module.ZoneName].ModuleResults.Add(result);
                }
            }

            return View(pageModel);
        }
        public void ExecuteModule_Provides_Context_Data_To_Executed_ModuleApplication()
        {
            // Arrange
            ModuleExecutionEngine engine = CreateExecutionEngine();
            HttpContextBase httpContext = Mockery.CreateMockHttpContext();
            Module module = new Module() { ModuleApplicationId = ModuleControllerTests.TestModule2Id };
            const string route = "Foo/Bar/Baz";

            ModuleRequestContext providedContext = null;
            Mock.Get(engine.ModuleApplications[ModuleControllerTests.TestModule2Id].GetExportedObject())
                .Setup(app => app.ExecuteRequest(It.IsAny<ModuleRequestContext>()))
                .Callback<ModuleRequestContext>(c => providedContext = c);

            // Act
            engine.ExecuteModule(httpContext, module, route);

            // Assert
            Assert.AreSame(engine.ModuleApplications[ModuleControllerTests.TestModule2Id].GetExportedObject(), providedContext.Application);
            Assert.AreSame(module, providedContext.Module);
            Assert.AreSame(httpContext, providedContext.HttpContext);
            Assert.AreEqual(route, providedContext.ModuleRoutingUrl);
        }