Exemplo n.º 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;
                }
            }
        }
Exemplo n.º 2
0
        public virtual ActionResult ProcessModule(ModulePosition pos)
        {
            this.ValueProvider.GetValue("q");

            if (IsGet())
            {
                var serializer = new JavaScriptSerializer();
                var manager    = ServiceFactory.GetService <ModuleManager>();
                var modules    = manager.AllModulesForSite(Site.FullName);
                var model      = new List <NameValueCollection>();
                foreach (var name in modules)
                {
                    var item = new NameValueCollection();
                    item.Add("ModuleName", name);
                    model.Add(item);
                    var moduleContext = new ModuleContext(name, Site);
                    var moduleInfo    = new ModuleContext(name, Site).ModuleInfo;
                    var setting       = moduleContext.GetModuleSettings();
                    if (setting != null && setting.Entry != null)
                    {
                        item["LinkToEntryName"] = setting.Entry.LinkToEntryName;
                        item["EntryName"]       = setting.Entry.Name;
                        item.Add("EntryAction", setting.Entry.Action);
                        item.Add("EntryController", setting.Entry.Controller);
                        item.Add("Values", setting.Entry.Values == null ? "[]" : serializer.Serialize(setting.Entry.Values.ToList()));
                    }
                    if (moduleInfo.EntryOptions != null)
                    {
                        var options = new List <object>();
                        foreach (var op in moduleInfo.EntryOptions)
                        {
                            options.Add(new
                            {
                                LinkToEntryName = op.Entry.LinkToEntryName,
                                Name            = op.Name,
                                EntryAction     = op.Entry.Action,
                                EntryController = op.Entry.Controller,
                                Values          = op.Entry.Values == null ? new List <KeyValuePair <string, object> >() : op.Entry.Values.ToList()
                            });
                        }

                        var optionsJson = serializer.Serialize(options);
                        item.Add("EntryOptions", optionsJson);
                    }
                }
                return(View(model));
            }

            if (pos.Entry != null && pos.Entry.Values != null)
            {
                var newValues = new RouteValueDictionary();
                foreach (var item in pos.Entry.Values)
                {
                    if (item.Value is string[])
                    {
                        newValues[item.Key] = ((string[])item.Value).FirstOrDefault();
                    }
                    else
                    {
                        newValues[item.Key] = item.Value;
                    }
                }
                pos.Entry.Values = newValues;
            }

            return(Json(new { html = new PageDesignModuleContent(pos).ToHtmlString() }));
        }