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); }
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); }
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); }
// 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); }