/// <summary> /// 初始化系统菜单 /// </summary> private void InitSysMenus(string controllerAssemblyName) { var assembly = Assembly.Load(controllerAssemblyName); var types = assembly?.GetTypes(); var list = types?.Where(t => !t.IsAbstract && t.IsPublic && t.IsSubclassOf(typeof(Controller))).ToList(); if (list != null) { foreach (var type in list) { var controllerName = type.Name.Replace("Controller", ""); var methods = type.GetMethods().Where(m => m.IsPublic && (m.ReturnType == typeof(IActionResult) || m.ReturnType == typeof(Task <IActionResult>))); var parentIdentity = $"{controllerName}"; if (menuRepository.Count(m => m.Identity.Equals(parentIdentity, StringComparison.OrdinalIgnoreCase)) == 0) { menuRepository.Add(new SysMenu() { MenuName = type.GetCustomAttribute <ControllerDescriptionAttribute>()?.Name ?? parentIdentity, Activable = true, Visiable = true, Identity = parentIdentity, RouteUrl = "", ParentId = String.Empty }); } foreach (var method in methods) { var identity = $"{controllerName}/{method.Name}"; if (menuRepository.Count(m => m.Identity.Equals(identity, StringComparison.OrdinalIgnoreCase)) == 0) { menuRepository.Add(new SysMenu() { MenuName = method.GetCustomAttribute <ActionDescriptionAttribute>()?.Name ?? method.Name, Activable = true, Visiable = method.GetCustomAttribute <AjaxRequestOnlyAttribute>() == null, Identity = identity, RouteUrl = identity, ParentId = identity.Equals(parentIdentity, StringComparison.OrdinalIgnoreCase) ? String.Empty : menuRepository.GetSingleOrDefault(x => x.Identity == parentIdentity)?.Id }); } } } } }