Пример #1
0
        /// <summary>
        /// Get action associated with path and method<br/>
        /// 获取路径和方法关联的Action函数<br/>
        /// </summary>
        public Func <IActionResult> Get(string path, string method)
        {
            // Search normal action
            var key = Pair.Create(path, method);

            if (Actions.TryGetValue(key, out var action))
            {
                return(action);
            }
            // Search regex action
            // Matching from the lonest base path, for example:
            //	rules: "get/some/{id}", "get/{id}"
            //	"get/some/1" matches "get/some/{id}"
            //	"get/1" matches "get/{id}"
            RegexActionsLock.EnterReadLock();
            try
            {
                var nodes = RegexActions.Travel(path, false).Where(a => a.HasValue).Reverse();
                foreach (var node in nodes)
                {
                    foreach (var descriptor in node.Value)
                    {
                        // Check method first because all methods are using the same list
                        if (descriptor.Method != method)
                        {
                            continue;
                        }
                        // Match pattern, descriptors should be sorted by pattern length descennding
                        var match = descriptor.Regex.Match(path);
                        if (!match.Success)
                        {
                            continue;
                        }
                        // Set custom parameters to request
                        var request = HttpManager.CurrentContext.Request;
                        for (var i = 0; i < descriptor.Parameters.Count; ++i)
                        {
                            var name  = descriptor.Parameters[i];
                            var value = match.Groups[i + 1].Value;
                            request.CustomParameters[name] = value;
                        }
                        return(descriptor.Action);
                    }
                }
            }
            finally
            {
                RegexActionsLock.ExitReadLock();
            }
            return(null);
        }