Ejemplo n.º 1
0
        /// <summary>
        /// 获取可能与指定路由规则冲突的路由规则
        /// </summary>
        /// <param name="rule">要检查冲突的路由规则</param>
        /// <returns>可能与之冲突的路由规则</returns>
        public SimpleRouteRule GetConflictRule(SimpleRouteRule rule)
        {
            var virtualPath = rule.GetVirtualPathDescriptor();
            var routeValues = rule.GetRouteValuesDescriptor();

            return(GetConflict(virtualPath, routeValues));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 在冲突检测表中添加一条记录,并检测与现有规则是否冲突。
        /// </summary>
        /// <param name="rule">要添加的规则</param>
        /// <param name="conflictRule">与之相冲突的规则,如果有的话</param>
        /// <returns>是否成功</returns>
        public bool AddRuleAndCheckConflict(SimpleRouteRule rule, out SimpleRouteRule conflictRule)
        {
            var virtualPath = rule.GetVirtualPathDescriptor();
            var routeValues = rule.GetRouteValuesDescriptor();

            lock ( _sync )
            {
                if (virtualPathList.TryGetValue(virtualPath, out conflictRule))
                {
                    return(false);
                }

                if (routeValuesList.TryGetValue(routeValues, out conflictRule))
                {
                    return(false);
                }


                conflictRule = null;
                virtualPathList.Add(virtualPath, rule);
                routeValuesList.Add(routeValues, rule);

                return(true);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 检查两个路由规则是否互斥。
        /// </summary>
        /// <param name="rule1">路由规则1</param>
        /// <param name="rule2">路由规则2</param>
        /// <returns></returns>
        public static bool Mutex(SimpleRouteRule rule1, SimpleRouteRule rule2)
        {
            var KeySet  = new HashSet <string>(rule1.StaticRouteValues.Keys, StringComparer.OrdinalIgnoreCase);
            var KeySet2 = new HashSet <string>(rule2.StaticRouteValues.Keys, StringComparer.OrdinalIgnoreCase);

            KeySet.IntersectWith(KeySet2);

            return(KeySet.Any(key => !rule1.StaticRouteValues[key].Equals(rule2.StaticRouteValues[key], StringComparison.OrdinalIgnoreCase)));
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 创建 VirtualPathData 对象
        /// </summary>
        /// <param name="virtualPath">虚拟路径</param>
        /// <param name="rule">产生该虚拟路径的路由规则</param>
        /// <returns>VirtualPathData 对象</returns>
        protected VirtualPathData CreateVirtualPathData(string virtualPath, SimpleRouteRule rule)
        {
            var data = new VirtualPathData(this, virtualPath);


            foreach (var pair in rule.DataTokens)
            {
                data.DataTokens.Add(pair.Key, pair.Value);
            }

            data.DataTokens["RoutingRuleName"] = rule.Name;

            return(data);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// 添加一个路由规则
        /// </summary>
        /// <param name="rule">路由规则</param>
        protected virtual SimpleRouteRule AddRule(SimpleRouteRule rule)
        {
            SimpleRouteRule conflictRule;

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

            _rules.Add(rule);

            rule.SimpleRouteTable = this;

            return(rule);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// 比较两个路由规则约束是否一致
        /// </summary>
        /// <param name="rule">要比较的路由规则</param>
        /// <returns>两个规则的约束是否一致</returns>
        public bool EqualsConstraints(SimpleRouteRule rule)
        {
            if (rule == null)
            {
                throw new ArgumentNullException("rule");
            }


            if (rule.StaticRouteValues.Count != StaticRouteValues.Count)
            {
                return(false);
            }

            return(IsMatch(rule.StaticRouteValues));
        }
Ejemplo n.º 7
0
        /// <summary>
        /// 添加一个路由规则
        /// </summary>
        /// <param name="name">规则名称</param>
        /// <param name="urlPattern">URL 模式</param>
        /// <param name="routeValues">静态/默认路由值</param>
        /// <param name="queryKeys">可用于 QueryString 的参数,若为null则表示无限制</param>
        public virtual SimpleRouteRule AddRule(string name, string urlPattern, IDictionary <string, string> routeValues, string[] queryKeys)
        {
            if (urlPattern == null)
            {
                throw new ArgumentNullException("urlPattern");
            }

            if (routeValues == null)
            {
                throw new ArgumentNullException("routeValues");
            }

            var rule = new SimpleRouteRule(name, urlPattern, routeValues, queryKeys);

            return(AddRule(rule));
        }
Ejemplo n.º 8
0
        /// <summary>
        /// 尝试冲突检测表中添加一条记录,并检测与现有规则是否冲突,若有冲突则无法添加成功
        /// </summary>
        /// <param name="rule">要添加的规则</param>
        /// <param name="conflictRule">与之相冲突的规则,如果有的话</param>
        /// <returns>是否添加成功</returns>
        public bool TryAddRule(SimpleRouteRule rule, out SimpleRouteRule conflictRule)
        {
            var virtualPath = rule.GetVirtualPathDescriptor();
            var routeValues = rule.GetRouteValuesDescriptor();

            lock ( _sync )
            {
                conflictRule = GetConflict(virtualPath, routeValues);
                if (conflictRule != null)
                {
                    return(false);
                }


                virtualPathList.Add(virtualPath, rule);
                routeValuesList.Add(routeValues, rule);
                return(true);
            }
        }
Ejemplo n.º 9
0
        /// <summary>
        /// 添加一个路由规则
        /// </summary>
        /// <param name="name">规则名称</param>
        /// <param name="urlPattern">URL 模式</param>
        /// <param name="routeValues">静态/默认路由值</param>
        /// <param name="queryKeys">可用于 QueryString 的参数,若为null则表示无限制</param>
        public virtual SimpleRouteRule AddRule(string name, string verb, bool oneway, string urlPattern, IDictionary <string, string> routeValues, IReadOnlyCollection <string> queryKeys)
        {
            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }

            if (urlPattern == null)
            {
                throw new ArgumentNullException(nameof(urlPattern));
            }

            if (routeValues == null)
            {
                throw new ArgumentNullException(nameof(routeValues));
            }



            if (urlPattern.StartsWith("~/") == false)
            {
                if (urlPattern.StartsWith("/"))
                {
                    throw new ArgumentException("urlPattern has invalid format", "urlPattern");
                }

                urlPattern = "~/" + urlPattern;
            }


            urlPattern = Regex.Replace(urlPattern, "/+", "/");

            if (urlPattern.Any() && urlPattern.EndsWith("/") == false)
            {
                urlPattern += "/";
            }

            var rule = new SimpleRouteRule(name, urlPattern, null, oneway, routeValues, queryKeys);

            return(AddRule(rule));
        }
Ejemplo n.º 10
0
    /// <summary>
    /// 在冲突检测表中添加一条记录,并检测与现有规则是否冲突。
    /// </summary>
    /// <param name="rule">要添加的规则</param>
    /// <param name="conflictRule">与之相冲突的规则,如果有的话</param>
    /// <returns>是否成功</returns>
    public bool AddRuleAndCheckConflict( SimpleRouteRule rule, out SimpleRouteRule conflictRule )
    {

      var virtualPath = rule.GetVirtualPathDescriptor();
      var routeValues = rule.GetRouteValuesDescriptor();

      lock ( _sync )
      {

        if ( virtualPathList.TryGetValue( virtualPath, out conflictRule ) )
          return false;

        if ( routeValuesList.TryGetValue( routeValues, out conflictRule ) )
          return false;


        conflictRule = null;
        virtualPathList.Add( virtualPath, rule );
        routeValuesList.Add( routeValues, rule );

        return true;
      }
    }
Ejemplo n.º 11
0
    /// <summary>
    /// 检查两个路由规则是否互斥。
    /// </summary>
    /// <param name="rule1">路由规则1</param>
    /// <param name="rule2">路由规则2</param>
    /// <returns></returns>
    public static bool Mutex( SimpleRouteRule rule1, SimpleRouteRule rule2 )
    {

      var KeySet = new HashSet<string>( rule1.StaticRouteValues.Keys, StringComparer.OrdinalIgnoreCase );
      var KeySet2 = new HashSet<string>( rule2.StaticRouteValues.Keys, StringComparer.OrdinalIgnoreCase );

      KeySet.IntersectWith( KeySet2 );

      return KeySet.Any( key => !rule1.StaticRouteValues[key].EqualsIgnoreCase( rule2.StaticRouteValues[key] ) );
    }
Ejemplo n.º 12
0
        /// <summary>
        /// 添加一个路由规则
        /// </summary>
        /// <param name="rule">路由规则</param>
        protected virtual SimpleRouteRule AddRule( SimpleRouteRule rule )
        {
            SimpleRouteRule conflictRule;
              if ( !AddRuleAndCheckConflict( rule, out conflictRule ) )
            throw new InvalidOperationException( string.Format( "添加规则\"{0}\"失败,路由表 \"{1}\" 中已经存在一条可能冲突的规则:\"{2}\"", rule.Name, conflictRule.SimpleRouteTable.Name, conflictRule.Name ) );

              _rules.Add( rule );

              rule.SimpleRouteTable = this;

              return rule;
        }
Ejemplo n.º 13
0
        /// <summary>
        /// 添加一个路由规则
        /// </summary>
        /// <param name="name">规则名称</param>
        /// <param name="urlPattern">URL 模式</param>
        /// <param name="routeValues">静态/默认路由值</param>
        /// <param name="queryKeys">可用于 QueryString 的参数,若为null则表示无限制</param>
        public virtual SimpleRouteRule AddRule( string name, string urlPattern, IDictionary<string, string> routeValues, string[] queryKeys )
        {
            if ( urlPattern == null )
            throw new ArgumentNullException( "urlPattern" );

              if ( routeValues == null )
            throw new ArgumentNullException( "routeValues" );

              var rule = new SimpleRouteRule( name, urlPattern, routeValues, queryKeys );

              return AddRule( rule );
        }
Ejemplo n.º 14
0
 /// <summary>
 /// 在冲突检测表中添加一条规则并检查冲突
 /// </summary>
 /// <param name="rule">要添加的规则</param>
 /// <param name="conflictRule">与之相冲突的规则</param>
 /// <returns>是否添加成功</returns>
 public static bool AddRuleAndCheckConflict( SimpleRouteRule rule, out SimpleRouteRule conflictRule )
 {
     return conflictCheckList.AddRuleAndCheckConflict( rule, out conflictRule );
 }
Ejemplo n.º 15
0
    /// <summary>
    /// 比较两个路由规则约束是否一致
    /// </summary>
    /// <param name="rule">要比较的路由规则</param>
    /// <returns>两个规则的约束是否一致</returns>
    public bool EqualsConstraints( SimpleRouteRule rule )
    {

      if ( rule == null )
        throw new ArgumentNullException( "rule" );


      if ( rule.StaticRouteValues.Count != StaticRouteValues.Count )
        return false;

      return IsMatch( rule.StaticRouteValues );

    }
Ejemplo n.º 16
0
        /// <summary>
        /// 创建 VirtualPathData 对象
        /// </summary>
        /// <param name="virtualPath">虚拟路径</param>
        /// <param name="rule">产生该虚拟路径的路由规则</param>
        /// <returns>VirtualPathData 对象</returns>
        protected VirtualPathData CreateVirtualPathData(VirtualPathContext context, string virtualPath, SimpleRouteRule rule)
        {
            virtualPath = Regex.Replace(virtualPath, "^~", context.HttpContext.Request.PathBase);

            var data = new VirtualPathData(this, virtualPath);

            foreach (var pair in rule.DataTokens)
            {
                data.DataTokens.Add(pair.Key, pair.Value);
            }

            data.DataTokens["RoutingRuleName"] = rule.Name;

            return(data);
        }
Ejemplo n.º 17
0
 /// <summary>
 /// 在冲突检测表中添加一条规则并检查冲突
 /// </summary>
 /// <param name="rule">要添加的规则</param>
 /// <param name="conflictRule">与之相冲突的规则</param>
 /// <returns>是否添加成功</returns>
 public static bool AddRuleAndCheckConflict(SimpleRouteRule rule, out SimpleRouteRule conflictRule)
 {
     return(conflictCheckList.AddRuleAndCheckConflict(rule, out conflictRule));
 }
Ejemplo n.º 18
0
    /// <summary>
    /// 创建 VirtualPathData 对象
    /// </summary>
    /// <param name="virtualPath">虚拟路径</param>
    /// <param name="rule">产生该虚拟路径的路由规则</param>
    /// <returns>VirtualPathData 对象</returns>
    protected VirtualPathData CreateVirtualPathData( string virtualPath, SimpleRouteRule rule )
    {
      var data = new VirtualPathData( this, virtualPath );


      foreach ( var pair in rule.DataTokens )
        data.DataTokens.Add( pair.Key, pair.Value );

      data.DataTokens["RoutingRuleName"] = rule.Name;

      return data;
    }