Пример #1
0
        /// <summary>
        /// Remove action associated with path and method<br/>
        /// 删除路径和方法关联的Action函数<br/>
        /// </summary>
        public bool Remove(string path, string method)
        {
            var basePath = ParsePath(path, out var regex, out var parameters);

            if (regex == null)
            {
                // Normal action
                var key = Pair.Create(basePath, method);
                return(Actions.TryRemove(key, out var _));
            }
            else
            {
                // Regex action
                RegexActionsLock.EnterReadLock();
                try
                {
                    var pattern = regex.ToString();
                    return(RegexActions.TryGetValue(basePath, out var descriptors) &&
                           descriptors.RemoveAll(d => d.Method == method && d.Regex.ToString() == pattern) > 0);
                }
                finally
                {
                    RegexActionsLock.ExitReadLock();
                }
            }
        }
Пример #2
0
        /// <summary>
        /// Associate action with path and method<br/>
        /// 设置路径和方法关联的Action函数<br/>
        /// </summary>
        public void Set(string path, string method, Func <IActionResult> action, bool overrideExists)
        {
            // Parse path
            var basePath = ParsePath(path, out var pattern, out var parameters);
            // Duplicate check
            var key = Pair.Create(basePath, method);

            if (!overrideExists)
            {
                bool duplicated = false;
                if (pattern == null)
                {
                    // No expression found, just check the key
                    duplicated = Actions.ContainsKey(key);
                }
                else
                {
                    // Check if there any descriptor have same method and regex
                    RegexActionsLock.EnterReadLock();
                    try {
                        duplicated = RegexActions.TryGetValue(basePath, out var descriptors) &&
                                     descriptors.Any(d => d.Method == method && d.Pattern == pattern);
                    } finally {
                        RegexActionsLock.ExitReadLock();
                    }
                }
                if (duplicated)
                {
                    throw new ArgumentException(
                              $"Action for method '{method}' and path '{path}' already registered, " +
                              "please use option `overrideExists` if you want to replace it");
                }
            }
            // Register action
            if (pattern == null)
            {
                // Normal action
                Actions[key] = action;
            }
            else
            {
                // Regex action
                RegexActionsLock.EnterWriteLock();
                try {
                    if (!RegexActions.TryGetValue(basePath, out var descriptors))
                    {
                        descriptors            = new List <RegexActionDescriptor>();
                        RegexActions[basePath] = descriptors;
                    }
                    // Sort descriptors by pattern length descending
                    var regex = new Regex(pattern);
                    descriptors.Add(new RegexActionDescriptor(method, pattern, regex, parameters, action));
                    descriptors.Sort((x, y) => y.Pattern.Length - x.Pattern.Length);
                } finally {
                    RegexActionsLock.ExitWriteLock();
                }
            }
        }