/// <summary> /// Register all modules /// </summary> public static void RegisterModules(RouteCollection routes, ITypeContainer container) { var typeSearcher = TypeSearcher.Instance(); var moduleTypes = typeSearcher.SearchTypesByBaseType(typeof(IModule)); Modules = new Dictionary <string, ModuleDescriptor>(); foreach (var moduleType in moduleTypes) { if (moduleType.IsAbstract) { continue; } var module = (IModule)Activator.CreateInstance(moduleType); if (module != null && string.IsNullOrEmpty(module.Name)) { continue; } if (Modules.ContainsKey(module.Name)) { continue; } var _asmName = moduleType.Assembly.GetName(); var moduleDescriptor = new ModuleDescriptor() { Name = module.Name, AssemblyQualifiedName = moduleType.AssemblyQualifiedName, AssemblyFullName = _asmName.FullName, AssemblyName = _asmName.Name }; moduleDescriptor.LoadAssemblyInfo(); ServiceModule serviceModule = module as ServiceModule; if (serviceModule != null) { serviceModule.RegisterTypes(container); serviceModule.RegisterRoutes(RouteTable.Routes); } SolutionModule solutionModule = module as SolutionModule; if (solutionModule != null && !solutionModule.DisableAutoRoutesRegistration) { #region Auto route generation var routeName = string.Format("solution_{0}", module.Name); var additional_ns = solutionModule.GetNamespaces(); var ns = new List <string>(); ns.Add(moduleType.Namespace); if (additional_ns != null && additional_ns.Length > 0) { ns.AddRange(additional_ns); } if (routes[routeName] == null) { moduleDescriptor.RouteName = routeName; var ctrls = moduleType.Assembly.GetTypes().Where(t => t.BaseType != null && t.BaseType.Equals(typeof(Controller))).ToList(); if (ctrls.Count > 0) { var hostDashboardType = Type.GetType("DNA.Web.HostDashboardAttribute,DNA.Web.ServiceModel"); var myDashboardType = Type.GetType("DNA.Web.MyDashboardAttribute,DNA.Web.ServiceModel"); foreach (var ctrl in ctrls) { var dashboardMethods = ctrl.GetMethods().Where(m => m.IsDefined(hostDashboardType, false)); var myDashboards = ctrl.GetMethods().Where(m => m.IsDefined(myDashboardType, false)); foreach (var m in dashboardMethods) { var ctrlName = ctrl.Name.Replace("Controller", ""); var dashboardRouteName = "host_module_" + module.Name + "_" + ctrlName + "_" + m.Name; if (RouteTable.Routes[dashboardRouteName] == null) { routes.MapRoute(dashboardRouteName, "{host}/{solution}/{controller}/{action}/{id}", new { id = UrlParameter.Optional }, new { host = "host", solution = module.Name, controller = ctrlName, action = m.Name }); } } foreach (var m in myDashboards) { var ctrlName = ctrl.Name.Replace("Controller", ""); var myRouteName = "mysite_" + module.Name + "_" + ctrlName + "_" + m.Name; if (RouteTable.Routes[myRouteName] == null) { routes.MapRoute(myRouteName, "{mysite}/{solution}/{controller}/{action}/{id}", new { id = UrlParameter.Optional }, new { mysite = "mysite", solution = module.Name, controller = ctrlName, action = m.Name }); } } } if (RouteTable.Routes[routeName] == null) { routes.MapRoute(routeName, "{website}/{locale}/{solution}/{controller}/{action}/{id}", new { action = "index", id = UrlParameter.Optional }, new { solution = module.Name, locale = @"([a-z]{2})-([a-z]{2}|[A-Z]{2})" }); } } } #endregion } Modules.Add(module.Name, moduleDescriptor); } container.Apply(); }
private static IEnumerable <DashboardGroup> GetItemGroups <T>(WebContext context) where T : DashboardBaseAttribute { var groups = new List <DashboardGroup>(); var items = new List <DashboardItem>(); var url = UrlUtility.CreateUrlHelper(); var typeSearcher = TypeSearcher.Instance(); var controllers = typeSearcher.SearchTypesByBaseType(typeof(Controller)); foreach (Type controller in controllers) { var methods = controller.GetMethods(BindingFlags.Public | BindingFlags.Instance); var actions = from MethodInfo method in methods where (method.GetCustomAttributes(typeof(T), true).Length > 0) select method; var sol = ModuleRegistration.Modules.Select(s => s.Value).FirstOrDefault(s => !string.IsNullOrEmpty(s.RouteName) && s.AssemblyFullName.Equals(controller.Assembly.FullName)); foreach (MethodInfo action in actions) { T attr = (T)Attribute.GetCustomAttribute(action, typeof(T)); string text = attr.Text; var securityAttr = Attribute.GetCustomAttribute(action, typeof(SecurityActionAttribute)); if (securityAttr != null) { if (!context.HasPermisson(controller, action.Name)) { continue; } } var httpPostAttr = Attribute.GetCustomAttribute(action, typeof(HttpPostAttribute)); if (httpPostAttr != null) { continue; } if (string.IsNullOrEmpty(attr.Text) && string.IsNullOrEmpty(attr.ResKey)) { continue; } if (!string.IsNullOrEmpty(attr.ResKey)) { if (!string.IsNullOrEmpty(attr.ResBaseName)) { text = App.GetResourceString(attr.ResBaseName, attr.ResKey); } else { text = App.GetResourceString(attr.ResKey); } } string _controller = controller.Name.Replace("Controller", ""); var curGroup = groups.FirstOrDefault(g => g.Name.Equals(attr.Group, StringComparison.OrdinalIgnoreCase)); if (curGroup == null) { curGroup = new DashboardGroup() { Name = attr.Group }; groups.Add(curGroup); } if (!string.IsNullOrEmpty(attr.GroupResKey) && !string.IsNullOrEmpty(attr.ResBaseName) && string.IsNullOrEmpty(curGroup.Title)) { curGroup.Title = App.GetResourceString(attr.ResBaseName, attr.GroupResKey); } var area = GetArea(controller); var _item = new DashboardItem() { Order = attr.Sequence, Title = text, Icon = attr.Icon, Action = action.Name, Controller = _controller, Area = area, ImageUrl = string.IsNullOrEmpty(attr.Icon) ? url.Content("~/content/images/icon_process_16.png") : url.Content("~/content/images/" + attr.Icon), NavigationUrl = url.Action(action.Name, _controller, new { Area = area, website = context.Website, locale = context.Locale }) }; var _routeName = attr.RouteName; if (sol != null && string.IsNullOrEmpty(_routeName)) { if (typeof(T) == typeof(HostDashboardAttribute)) { _routeName = "host_module_" + sol.Name + "_" + _controller + "_" + action.Name; } else { if (typeof(T) == typeof(MyDashboardAttribute)) { _routeName = "mysite_" + sol.Name + "_" + _controller + "_" + action.Name; } else { _routeName = sol.RouteName; } } } if (!string.IsNullOrEmpty(_routeName)) { var route = RouteTable.Routes[_routeName] as Route; var routeValues = new RouteValueDictionary(new { Area = area, website = context.Website, action = action.Name, controller = _controller, locale = context.Locale }); var path = url.RouteUrl(_routeName, routeValues); if (string.IsNullOrEmpty(path)) { path = route.Url; foreach (string key in route.Defaults.Keys) { path = path.Replace("{" + key + "}", routeValues.Keys.Contains(key) ? routeValues[key].ToString() : route.Defaults[key].ToString()); } foreach (string key in route.Constraints.Keys) { var constr = route.Constraints[key].ToString(); if (!constr.StartsWith("(")) { path = path.Replace("{" + key + "}", constr); } } foreach (string key in routeValues.Keys) { path = path.Replace("{" + key + "}", routeValues[key].ToString()); } } _item.NavigationUrl = url.Content("~/" + path); } if (curGroup.Items.Count(i => i.NavigationUrl.Equals(_item.NavigationUrl, StringComparison.OrdinalIgnoreCase)) == 0) { curGroup.Items.Add(_item); } } } return(groups.OrderBy(g => g.Name).ToList()); }
private static void RegisterSpecifiedTypes() { var typeSearcher = TypeSearcher.Instance(); var unityConfigFile = HttpRuntime.AppDomainAppPath + "unity.config"; var unity = XDocument.Load(unityConfigFile); var ns = unity.Root.GetDefaultNamespace(); var isChanged = false; var types = typeSearcher.SearchTypesOfAttribute(typeof(InjectAttribute), false); foreach (var type in types) { var typeName = type.Name; var typeFullName = type.FullName + "," + type.Assembly.GetName().Name; var asmName = type.Assembly.GetName().Name; var typeNS = type.Namespace; var mapTo = ""; var iocAttr = typeSearcher.ExtractCustomAttribute <InjectAttribute>(type); if (!string.IsNullOrEmpty(iocAttr.Alias)) { typeName = iocAttr.Alias; } if (!string.IsNullOrEmpty(iocAttr.MapTo)) { mapTo = iocAttr.MapTo; } //Register alias name var aliasNode = unity.Root.Elements(ns + "alias").FirstOrDefault(e => e.StrAttr("alias").Equals(typeName)); if (aliasNode == null) { unity.Root.Add(new XElement(ns + "alias", new XAttribute("alias", typeName), new XAttribute("type", typeFullName))); isChanged = true; } //Register namespace if (unity.Root.Elements(ns + "namespace").FirstOrDefault(e => e.StrAttr("name").Equals(typeNS)) == null) { unity.Root.Add(new XElement(ns + "namespace", new XAttribute("name", typeNS))); isChanged = true; } //Register assembly if (unity.Root.Elements(ns + "assembly").FirstOrDefault(e => e.StrAttr("name").Equals(asmName)) == null) { unity.Root.Add(new XElement(ns + "assembly", new XAttribute("name", asmName))); isChanged = true; } //Register types if (!string.IsNullOrEmpty(mapTo)) { var aliasName = typeName[0].ToString().ToLower() + typeName.Substring(1); if (unity.Root.Descendants(ns + "register").FirstOrDefault(e => e.StrAttr("type").Equals(mapTo) && e.StrAttr("mapTo").Equals(typeName)) == null) { unity.Root .Element(ns + "container") .Add(new XElement(ns + "register", new XAttribute("type", mapTo), new XAttribute("mapTo", typeName), new XAttribute("name", aliasName))); isChanged = true; } } } if (isChanged) { unity.Save(unityConfigFile); } }