/// <summary> /// Returns the route collection as a read-only dictionary mapping configured names to routes. /// </summary> /// <param name="routes">The <see cref="HttpRouteCollection">route collection</see> to convert.</param> /// <returns>A new <see cref="IReadOnlyDictionary{TKey, TValue}">read-only dictonary</see> of /// <see cref="IHttpRoute">routes</see> mapped to their name.</returns> public static IReadOnlyDictionary <string, IHttpRoute> ToDictionary(this HttpRouteCollection routes) { Arg.NotNull(routes, nameof(routes)); Contract.Ensures(Contract.Result <IReadOnlyDictionary <string, IHttpRoute> >() != null); const string HostedHttpRouteCollection = "System.Web.Http.WebHost.Routing.HostedHttpRouteCollection"; try { return(routes.CopyToDictionary()); } catch (NotSupportedException) when(routes.GetType().FullName == HostedHttpRouteCollection) { return(routes.BuildDictionaryFromKeys()); } }
/// <summary> /// Returns the route collection as a read-only dictionary mapping configured names to routes. /// </summary> /// <param name="routes">The <see cref="HttpRouteCollection">route collection</see> to convert.</param> /// <returns>A new <see cref="IReadOnlyDictionary{TKey, TValue}">read-only dictonary</see> of /// <see cref="IHttpRoute">routes</see> mapped to their name.</returns> public static IReadOnlyDictionary <string, IHttpRoute> ToDictionary(this HttpRouteCollection routes) { if (routes == null) { throw new ArgumentNullException(nameof(routes)); } const string HostedHttpRouteCollection = "System.Web.Http.WebHost.Routing.HostedHttpRouteCollection"; try { return(routes.CopyToDictionary()); } catch (NotSupportedException) when(routes.GetType().FullName == HostedHttpRouteCollection) { return(routes.BuildDictionaryFromKeys()); } }