private void LoadServiceRoute(Type t, RouteAttribute a1)
        {
            RoutingService obj = new RoutingService {
                Attr        = a1,
                ServiceType = t
            };

            _regexList.Add(obj);
        }
Exemplo n.º 2
0
        public static bool IsMatchRequest(HttpContext context, RoutingService route)
        {
            if (route.Attr.Regex == null)
            {
                throw new InvalidOperationException(/*  不可能发生的事情,除非是代码有BUG  */);
            }

            // 先检查类型中标记的[Route]是否匹配
            Match match = route.Attr.Regex.Match(context.Request.Path);

            if (match.Success == false)
            {
                return(false);
            }

            // 获取 action 名字
            string action = match.Groups["action"].Value;

            if (action == null)
            {
                // URL模式中没有指定 {action}占位符,就用HttpMethod来代替
                action = context.Request.HttpMethod;
            }


            // 在类型中查找匹配的 action
            MethodInfo method  = null;
            var        methods = route.ServiceType.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly);

            foreach (MethodInfo m in methods)
            {
                // RouteIgnoreAttribute 优先级最高。
                if (m.GetMyAttribute <RouteIgnoreAttribute>() != null)
                {
                    continue;
                }

                // 别名比较
                RouteActionAttribute a2 = m.GetMyAttribute <RouteActionAttribute>();
                if (a2 != null && string.IsNullOrEmpty(a2.Name) == false)
                {
                    if (action.EqualsIgnoreCase(a2.Name))
                    {
                        method = m;
                        break;
                    }
                }

                // 直接用方法名比较
                if (action.EqualsIgnoreCase(m.Name))
                {
                    method = m;
                    break;
                }
            }

            // 匹配成功
            if (method != null)
            {
                RoutingService obj = route.Clone(method);
                obj.SetHandler(context);

                context.Request.RegexMatch = match;                     // 保存匹配的结果
                return(true);
            }


            // 类型上标记的[Route("....")]匹配成功,但是没有找到合适的方法,所以直接退出整个搜索过程
            throw new RouteMatchExistException();
        }