// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.ComponentModel;
using System.Net.Http;
using System.Web.Http.Routing;
using System.Web.Http.WebHost.Routing;
using System.Web.Routing;
namespace System.Web.Http
{
///
/// Extension methods for
///
[EditorBrowsable(EditorBrowsableState.Never)]
public static class RouteCollectionExtensions
{
///
/// Maps the specified route template.
///
/// A collection of routes for the application.
/// The name of the route to map.
/// The route template for the route.
/// A reference to the mapped route.
public static Route MapHttpRoute(this RouteCollection routes, string name, string routeTemplate)
{
return MapHttpRoute(routes, name, routeTemplate, defaults: null, constraints: null, handler: null);
}
///
/// Maps the specified route template and sets default constraints, and namespaces.
///
/// A collection of routes for the application.
/// The name of the route to map.
/// The route template for the route.
/// An object that contains default route values.
/// A reference to the mapped route.
public static Route MapHttpRoute(this RouteCollection routes, string name, string routeTemplate, object defaults)
{
return MapHttpRoute(routes, name, routeTemplate, defaults, constraints: null, handler: null);
}
///
/// Maps the specified route template and sets default route values, constraints, and namespaces.
///
/// A collection of routes for the application.
/// The name of the route to map.
/// The route template for the route.
/// An object that contains default route values.
/// A set of expressions that specify values for .
/// A reference to the mapped route.
public static Route MapHttpRoute(this RouteCollection routes, string name, string routeTemplate, object defaults, object constraints)
{
return MapHttpRoute(routes, name, routeTemplate, defaults, constraints, handler: null);
}
///
/// Maps the specified route template and sets default route values, constraints, namespaces, and end-point message handler.
///
/// A collection of routes for the application.
/// The name of the route to map.
/// The route template for the route.
/// An object that contains default route values.
/// A set of expressions that specify values for .
/// The handler to which the request will be dispatched.
/// A reference to the mapped route.
public static Route MapHttpRoute(this RouteCollection routes, string name, string routeTemplate, object defaults, object constraints, HttpMessageHandler handler)
{
if (routes == null)
{
throw Error.ArgumentNull("routes");
}
HttpRouteValueDictionary defaultsDictionary = new HttpRouteValueDictionary(defaults);
HttpRouteValueDictionary constraintsDictionary = new HttpRouteValueDictionary(constraints);
HostedHttpRoute httpRoute = (HostedHttpRoute)GlobalConfiguration.Configuration.Routes.CreateRoute(routeTemplate, defaultsDictionary, constraintsDictionary, dataTokens: null, handler: handler);
Route route = httpRoute.OriginalRoute;
routes.Add(name, route);
return route;
}
}
}