Beispiel #1
0
        /// <summary>
        /// 添加一个路由规则
        /// </summary>
        /// <param name="rule">路由规则</param>
        protected void AddRule(SimpleRoutingRule rule)
        {
            {
                var conflictRule = _rules
                                   .Where(r => r.RouteKeys.Length == rule.RouteKeys.Length)               //若通过RouteKey多寡无法区分
                                   .Where(r => r.DynamicRouteKyes.Length == rule.DynamicRouteKyes.Length) //若通过动态路径段多寡也无法区分
                                   .Where(r => !SimpleRoutingRule.Mutex(r, rule))                         //若与现存规则不互斥
                                   .FirstOrDefault();

                if (conflictRule != null)
                {
                    throw new InvalidOperationException(string.Format("添加规则\"{0}\"失败,路由表中已经存在一条可能冲突的规则:\"{1}\"", rule.Name, conflictRule.Name));
                }
            }


            {
                var conflictRule = _rules
                                   .Where(r => r.Paragraphes.Length == rule.Paragraphes.Length)    //若路径段长度一致
                                   .Where(r => r.StaticPrefix.EqualsIgnoreCase(rule.StaticPrefix)) //若静态段也一致
                                   .FirstOrDefault();


                if (conflictRule != null)
                {
                    throw new InvalidOperationException(string.Format("添加规则\"{0}\"失败,路由表中已经存在一条可能冲突的规则:\"{1}\"", rule.Name, conflictRule.Name));
                }
            }

            _rules.Add(rule);
        }