Ejemplo n.º 1
0
        public override IController CreateController(System.Web.Routing.RequestContext requestContext, string controllerName)
        {
            try
            {
                Controller   controller   = base.CreateController(requestContext, controllerName) as Controller;
                ControllerEx controllerEx = controller as ControllerEx;
                if (controllerEx != null && controllerEx.EnableDynamicAction)
                {
                    controller.ActionInvoker = new DynamicActionInvoker()
                    {
                        ActionDelegate = controllerEx.OnDynamicActionInvoked
                    };
                }

                return(controller);
            }
            catch (Exception ex)
            {
                Logger.Exception(ex);
                return(null);
            }
        }
Ejemplo n.º 2
0
        public void LoadConfigration()
        {
            try
            {
                Logger.Information("SiteRouteInfo", "{0} LoadConfigration start", this.Domain.DistinctName);
                ContentTree tree = ContentTree.GetByDistinctName(this.Domain.DistinctName, this.Domain.TemplateDomainDistinctName, false);
                if (tree != null)
                {
                    RouteCollection routeCollection = new RouteCollection();
                    Dictionary <string, RouteExtraInfo> routeExtraInfos = new Dictionary <string, RouteExtraInfo>(StringComparer.InvariantCultureIgnoreCase);

                    Type[] types = ControllerEx.GetControllerAssembly().GetTypes();

                    foreach (KeyValuePair <string, ContentNode> item in tree.AllNodes)
                    {
                        ContentNode node = item.Value;
                        if (node.NodeType == ContentNode.ContentNodeType.Page)
                        {
                            PageNode pageNode = new PageNode(node);

                            if (string.IsNullOrWhiteSpace(pageNode.Controller))
                            {
                                continue;
                            }

                            // Get the controller class
                            Type controllerType = types.FirstOrDefault(t => t.FullName == pageNode.Controller);

                            if (controllerType == null)
                            {
                                Logger.Error("CMS", "Error, can't find the type [{0}].", pageNode.Controller);
                                continue;
                            }
                            else
                            {
                                object[] attributes = controllerType.GetCustomAttributes(typeof(ControllerExtraInfoAttribute), false);
                                ControllerExtraInfoAttribute attribute = null;
                                if (attributes.Length > 0)
                                {
                                    attribute = attributes[0] as ControllerExtraInfoAttribute;
                                }
                                else
                                {
                                    attribute = new ControllerExtraInfoAttribute()
                                    {
                                        DefaultAction = "Index"
                                    }
                                };

                                string url = pageNode.ContentNode.RelativePath;
                                if (!url.StartsWith("/"))
                                {
                                    url = "/" + url;
                                }
                                url = url.TrimEnd('/');

                                RouteExtraInfo extraInfo = new RouteExtraInfo();
                                extraInfo.RouteName = pageNode.RouteName;
                                extraInfo.RouteUrl  = string.Format("{0}/{{action}}/{1}", url.TrimStart('/').TrimEnd('/'), attribute.ParameterUrl);
                                Route route = routeCollection.MapRoute(extraInfo.RouteName, extraInfo.RouteUrl);
                                extraInfo.Action = attribute.DefaultAction.DefaultIfNullOrEmpty("Index");
                                route.Defaults.Add("action", extraInfo.Action);
                                extraInfo.Controller = Regex.Replace(controllerType.Name, "(Controller)$", string.Empty, RegexOptions.IgnoreCase | RegexOptions.ECMAScript | RegexOptions.CultureInvariant | RegexOptions.Compiled);
                                route.Defaults.Add("controller", extraInfo.Controller);

                                if (!string.IsNullOrEmpty(attribute.ParameterUrl))
                                {
                                    string[] parameters = attribute.ParameterUrl.Split('/');
                                    foreach (string parameter in parameters)
                                    {
                                        string p = parameter.TrimStart('{').TrimEnd('}');
                                        extraInfo.Parameters.Add(p);
                                        route.Defaults.Add(p, UrlParameter.Optional);
                                    }
                                }
                                extraInfo.Url                 = url;
                                extraInfo.ControllerType      = controllerType;
                                route.DataTokens["RouteName"] = extraInfo.RouteName;

                                routeExtraInfos[extraInfo.RouteName] = extraInfo;
                            } // if_else
                        }     // if
                    }         // foreach

                    this.RouteExtraInfos = routeExtraInfos;
                    this.RouteCollection = routeCollection;

                    ObjectHelper.BinarySerialize <Dictionary <string, RouteExtraInfo> >(routeExtraInfos, GetCacheFilePath());
                }// if

                Logger.Information("SiteRouteInfo", "{0} LoadConfigration completed", this.Domain.DistinctName);
            }
            catch (Exception ex)
            {
                Logger.Exception(ex);
            }
        }// LoadConfigration