예제 #1
0
        /// <summary>
        /// 注册路由
        /// </summary>
        /// <param name="methodInfo"></param>
        /// <param name="servicePath"></param>
        public void RegisterRestPaths(MethodInfo methodInfo, string servicePath)
        {
            //获取到RouteAttribute
            List <RouteAttribute> attrs = methodInfo.GetCustomAttributes(typeof(RouteAttribute), false).OfType <RouteAttribute>().ToList();
            //默认的路由前面要加上 "/"
            string defaultOperationRestPath = ("/" + methodInfo.Name).ToLower();

            //如果没有打RouteAttribute 加入一个默认的路由
            if (attrs.Count(a => a.Path.ToLower() == defaultOperationRestPath) == 0)
            {
                attrs.Add(new RouteAttribute(defaultOperationRestPath));
            }

            foreach (RouteAttribute attr in attrs)
            {
                var restPath = new RestPath(methodInfo, attr.Path, attr.Verbs, attr.Summary, attr.Notes);

                var defaultAttr = attr as FallbackRouteAttribute;
                if (defaultAttr != null)
                {
                    if (EndpointHost.Config != null)
                    {
                        if (EndpointHost.Config.FallbackRestPaths.ContainsKey(servicePath))
                        {
                            throw new NotSupportedException(string.Format(
                                                                "FallbackRouteAttribute is already defined. Only 1 [FallbackRoute] is allowed."));
                        }

                        EndpointHost.Config.FallbackRestPaths[servicePath] = (httpMethod, theServicePath, pathInfo, filePath) =>
                        {
                            var pathInfoParts = RestPath.GetPathPartsForMatching(pathInfo);
                            return(restPath.IsMatch(httpMethod, pathInfoParts) ? restPath : null);
                        };
                    }

                    continue;
                }

                if (!restPath.IsValid)
                {
                    throw new NotSupportedException(string.Format(
                                                        "RestPath '{0}' on method '{1}' is not Valid", attr.Path, methodInfo.Name));
                }
                //路由注册
                RegisterRestPath(restPath, servicePath);
            }
        }
예제 #2
0
        public IRestPath GetRestPathForRequest(string httpMethod, string servicePath, string pathInfo)
        {
            Dictionary <string, List <RestPath> > restPaths = RestPathMap[servicePath];

            var matchUsingPathParts = RestPath.GetPathPartsForMatching(pathInfo);

            List <RestPath> firstMatches;

            var yieldedHashMatches = RestPath.GetFirstMatchHashKeys(matchUsingPathParts);

            foreach (var potentialHashMatch in yieldedHashMatches)
            {
                if (!restPaths.TryGetValue(potentialHashMatch, out firstMatches))
                {
                    continue;
                }

                var bestScore = -1;
                foreach (var restPath in firstMatches)
                {
                    var score = restPath.MatchScore(httpMethod, matchUsingPathParts);
                    if (score > bestScore)
                    {
                        bestScore = score;
                    }
                }
                if (bestScore > 0)
                {
                    foreach (var restPath in firstMatches)
                    {
                        if (bestScore == restPath.MatchScore(httpMethod, matchUsingPathParts))
                        {
                            return(restPath);
                        }
                    }
                }
            }

            var yieldedWildcardMatches = RestPath.GetFirstMatchWildCardHashKeys(matchUsingPathParts);

            foreach (var potentialHashMatch in yieldedWildcardMatches)
            {
                if (!restPaths.TryGetValue(potentialHashMatch, out firstMatches))
                {
                    continue;
                }

                var bestScore = -1;
                foreach (var restPath in firstMatches)
                {
                    var score = restPath.MatchScore(httpMethod, matchUsingPathParts);
                    if (score > bestScore)
                    {
                        bestScore = score;
                    }
                }
                if (bestScore > 0)
                {
                    foreach (var restPath in firstMatches)
                    {
                        if (bestScore == restPath.MatchScore(httpMethod, matchUsingPathParts))
                        {
                            return(restPath);
                        }
                    }
                }
            }

            return(null);
        }