/// <summary> /// 获取routing中的缓存 /// 若不存在则创建 /// </summary> /// <param name="controllerType"></param> /// <param name="controllerName"></param> /// <param name="actionName"></param> /// <param name="isPost"></param> /// <returns></returns> public static Routing GetOrAdd(Type controllerType, string controllerName, string actionName, bool isPost) { lock (_locker) { var list = _list.Where(b => string.Compare(b.ControllerName, controllerName, true) == 0 && string.Compare(b.ActionName, actionName, true) == 0).ToList(); if (list == null || list.Count == 0 || list.FirstOrDefault(b => b.IsPost == isPost) == null) { var actions = controllerType.GetMethods().Where(b => string.Compare(b.Name, actionName, true) == 0).ToList(); if (actions == null || actions.Count == 0) { throw new Exception($"{controllerName}/{actionName}找不到此action!"); } else if (actions.Count > 2) { throw new Exception($"{controllerName}/{actionName}有多个重复的的action!"); } else { var instance = System.Activator.CreateInstance(controllerType); List <object> iAttrs = null; //类上面的过滤 var attrs = controllerType.GetCustomAttributes(true); if (attrs != null && attrs.Length > 0) { var actionAttrs = attrs.Where(b => b.GetType().BaseType.Name == ConstHelper.ACTIONFILTERATTRIBUTE).ToList(); if (actionAttrs != null && actionAttrs.Count > 0) { iAttrs = actionAttrs; } } foreach (var action in actions) { var routing = new Routing() { ControllerName = controllerName, ActionName = actionName, Instance = (Controller)instance, FilterAtrrs = iAttrs, Action = action, ActionInvoker = FastInvoke.GetMethodInvoker(action) }; //action上面的过滤 var actionAttrs = action.GetCustomAttributes(true); if (actionAttrs != null) { var filterAttrs = attrs.Where(b => b.GetType().BaseType.Name == ConstHelper.ACTIONFILTERATTRIBUTE).ToList(); if (filterAttrs != null && filterAttrs.Count > 0) { routing.ActionFilterAtrrs = filterAttrs; } var dGet = actionAttrs.Where(b => b.GetType().Name == ConstHelper.HTTPGET).FirstOrDefault(); if (dGet != null) { routing.IsPost = false; _list.Add(routing); } var dPost = actionAttrs.Where(b => b.GetType().Name == ConstHelper.HTTPPOST).FirstOrDefault(); if (dPost != null) { var routing2 = new Routing() { ControllerName = controllerName, ActionName = actionName, Instance = (Controller)instance, FilterAtrrs = iAttrs, Action = action, ActionInvoker = FastInvoke.GetMethodInvoker(action), ActionFilterAtrrs = routing.ActionFilterAtrrs }; routing2.IsPost = true; _list.Add(routing2); } } else { routing.IsPost = false; _list.Add(routing); } } return(_list.Where(b => string.Compare(b.ControllerName, controllerName, true) == 0 && string.Compare(b.ActionName, actionName, true) == 0 && b.IsPost == isPost).FirstOrDefault()); } } return(list.FirstOrDefault(b => b.IsPost == isPost)); } }
/// <summary> /// 获取routing中的缓存 /// 若不存在则创建 /// </summary> /// <param name="controllerType"></param> /// <param name="controllerName"></param> /// <param name="actionName"></param> /// <param name="isPost"></param> /// <returns></returns> public static Routing TryGet(Type controllerType, string controllerName, string actionName, bool isPost) { lock (_locker) { var list = _list.Where(b => b.ControllerName.ToLower() == controllerName.ToLower() && b.ActionName.ToLower() == actionName.ToLower() && b.IsPost == isPost).ToList(); if (list == null || list.Count == 0) { var routing = new Routing() { ControllerName = controllerName, ActionName = actionName, IsPost = isPost }; var actions = controllerType.GetMethods().Where(b => b.Name.ToLower() == actionName.ToLower()).ToList(); if (actions == null || actions.Count == 0) { throw new Exception($"{controllerName}/{actionName}找不到此action!"); } else if (actions.Count > 2) { throw new Exception($"{controllerName}/{actionName}有多个重复的的action!"); } else { routing.Instance = System.Activator.CreateInstance(controllerType); //类上面的过滤 var attrs = controllerType.GetCustomAttributes(true); if (attrs != null && attrs.Length > 0) { var actionAttrs = attrs.Where(b => b.GetType().BaseType.Name == "ActionFilterAttribute").ToList(); if (actionAttrs != null && actionAttrs.Count > 0) { routing.Atrrs = actionAttrs; } } else { routing.Atrrs = null; } routing.Action = actions[0]; //action上面的过滤 if (routing.Atrrs == null) { attrs = actions[0].GetCustomAttributes(true); if (attrs != null) { var actionAttrs = attrs.Where(b => b.GetType().BaseType.Name == "ActionFilterAttribute").ToList(); if (actionAttrs != null && actionAttrs.Count > 0) { routing.Atrrs = actionAttrs; } } else { routing.Atrrs = null; } } } _list.Add(routing); return(routing); } else if (list.Count > 1) { throw new Exception("500"); } return(list.FirstOrDefault()); } }