コード例 #1
0
        static KeyValuePair <string, IHttpRoute>[] CopyRouteEntries(HttpRouteCollection routes)
        {
            Contract.Requires(routes != null);
            Contract.Ensures(Contract.Result <KeyValuePair <string, IHttpRoute>[]>() != null);

            var items = new KeyValuePair <string, IHttpRoute> [routes.Count];

            try
            {
                routes.CopyTo(items, 0);
            }
            catch (NotSupportedException) when(routes.GetType().FullName == "System.Web.Http.WebHost.Routing.HostedHttpRouteCollection")
            {
                var keys = GetRouteKeys(routes);

                for (var i = 0; i < keys.Count; i++)
                {
                    var key   = keys[i];
                    var route = routes[key];

                    items[i] = new KeyValuePair <string, IHttpRoute>(key, route);
                }
            }

            return(items);
        }
コード例 #2
0
        static IReadOnlyDictionary <string, IHttpRoute> CopyToDictionary(this HttpRouteCollection routes)
        {
            var items = new KeyValuePair <string, IHttpRoute> [routes.Count];

            routes.CopyTo(items, 0);

            var dictionary = new Dictionary <string, IHttpRoute>(routes.Count, StringComparer.OrdinalIgnoreCase);

            for (var i = 0; i < items.Length; i++)
            {
                var item = items[i];
                dictionary[item.Key] = item.Value;
            }

            return(dictionary);
        }
コード例 #3
0
        internal static string GetRouteName(this HttpRouteCollection routes, IHttpRoute route)
        {
            Contract.Requires(routes != null);
            Contract.Requires(route != null);

            var items = new KeyValuePair <string, IHttpRoute> [routes.Count];

            routes.CopyTo(items, 0);

            foreach (var item in items)
            {
                if (Equals(item.Value, route))
                {
                    return(item.Key);
                }
            }

            return(null);
        }
コード例 #4
0
 // Normal route table enumeration doesn't include the route name.
 internal static IEnumerable <KeyValuePair <string, IHttpRoute> > GetRoutesWithNames(this HttpRouteCollection routes)
 {
     KeyValuePair <string, IHttpRoute>[] names = new KeyValuePair <string, IHttpRoute> [routes.Count];
     routes.CopyTo(names, 0);
     return(names);
 }