/// <summary> /// Maps the specified route template and sets default route values, constraints, and end-point message handler. /// </summary> /// <param name="routes">A collection of routes for the application.</param> /// <param name="name">The name of the route to map.</param> /// <param name="routeTemplate">The route template for the route.</param> /// <param name="defaults">An object that contains default route values.</param> /// <param name="constraints">A set of expressions that specify values for <paramref name="routeTemplate"/>.</param> /// <param name="handler">The handler to which the request will be dispatched.</param> /// <returns>A reference to the mapped route.</returns> public static IHttpRoute MapHttpRoute( this HttpRouteCollection 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 ); IHttpRoute route = routes.CreateRoute( routeTemplate, defaultsDictionary, constraintsDictionary, dataTokens: null, handler: handler ); routes.Add(name, route); return(route); }
public static void MapODataRoute(this HttpRouteCollection routes, string routeName, string routePrefix, IEdmModel model, IODataPathHandler pathHandler, IEnumerable <IODataRoutingConvention> routingConventions, ODataBatchHandler batchHandler) { if (routes == null) { throw Error.ArgumentNull("routes"); } if (!String.IsNullOrEmpty(routePrefix)) { int prefixLastIndex = routePrefix.Length - 1; if (routePrefix[prefixLastIndex] == '/') { // Remove the last trailing slash if it has one. routePrefix = routePrefix.Substring(0, routePrefix.Length - 1); } } if (batchHandler != null) { batchHandler.ODataRouteName = routeName; string batchTemplate = String.IsNullOrEmpty(routePrefix) ? ODataRouteConstants.Batch : routePrefix + '/' + ODataRouteConstants.Batch; routes.MapHttpBatchRoute(routeName + "Batch", batchTemplate, batchHandler); } ODataPathRouteConstraint routeConstraint = new ODataPathRouteConstraint(pathHandler, model, routeName, routingConventions); routes.Add(routeName, new ODataRoute(routePrefix, routeConstraint)); }
public static IHttpRoute IgnoreRoute( this HttpRouteCollection routes, string routeName, string routeTemplate, object constraints ) { if (routes == null) { throw new ArgumentNullException("routes"); } if (routeName == null) { throw new ArgumentNullException("routeName"); } if (routeTemplate == null) { throw new ArgumentNullException("routeTemplate"); } IgnoreHttpRouteInternal route = new IgnoreHttpRouteInternal( routeTemplate, new HttpRouteValueDictionary(constraints), new StopRoutingHandler() ); routes.Add(routeName, route); return(route); }
// Add generation hooks for the Attribute-routing subroutes. // This lets us generate urls for routes supplied by attr-based routing. private static void AddGenerationHooksForSubRoutes(HttpRouteCollection destRoutes, HttpSubRouteCollection sourceRoutes, HttpRouteBuilder routeBuilder) { foreach (KeyValuePair <string, IHttpRoute> kv in sourceRoutes.NamedRoutes) { string name = kv.Key; IHttpRoute route = kv.Value; var stubRoute = routeBuilder.BuildGenerationRoute(route); destRoutes.Add(name, stubRoute); } }
// Add generation hooks for the Attribute-routing subroutes. // This lets us generate urls for routes supplied by attr-based routing. private static void AddGenerationHooksForSubRoutes(HttpRouteCollection destRoutes, HttpRouteCollection sourceRoutes) { foreach (KeyValuePair <string, IHttpRoute> kv in sourceRoutes.GetRoutesWithNames()) { string name = kv.Key; IHttpRoute route = kv.Value; var stubRoute = new GenerateRoute(route); destRoutes.Add(name, stubRoute); } }
/// <summary> /// Maps the specified route template and sets default route values and constraints. /// </summary> /// <param name="routes">A collection of routes for the application.</param> /// <param name="name">The name of the route to map.</param> /// <param name="routeTemplate">The route template for the route.</param> /// <param name="defaults">An object that contains default route values.</param> /// <param name="constraints">A set of expressions that specify values for <paramref name="routeTemplate"/>.</param> /// <returns>A reference to the mapped route.</returns> public static IHttpRoute MapHttpRoute(this HttpRouteCollection routes, string name, string routeTemplate, object defaults, object constraints) { if (routes == null) { throw Error.ArgumentNull("routes"); } IHttpRoute route = routes.CreateRoute(routeTemplate, defaults, constraints, parameters: null); routes.Add(name, route); return(route); }
private static void AddRouteToRespondWithBadRequestWhenAtLeastOneRouteCouldMatch( string routeName, string routePrefix, HttpRouteCollection routes, List<ODataRoute> odataRoutes, List<IHttpRouteConstraint> unversionedConstraints ) { Contract.Requires( !IsNullOrEmpty( routeName ) ); Contract.Requires( routes != null ); Contract.Requires( odataRoutes != null ); Contract.Requires( unversionedConstraints != null ); var unversionedRoute = new ODataRoute( routePrefix, new UnversionedODataPathRouteConstraint( unversionedConstraints ) ); AddApiVersionConstraintIfNecessary( unversionedRoute ); routes.Add( routeName + UnversionedRouteSuffix, unversionedRoute ); odataRoutes.Add( unversionedRoute ); }
public void HttpRouteCollection_Dispose_UniquifiesHandlers() { // Arrange var collection = new HttpRouteCollection(); var handler1 = new Mock <HttpMessageHandler>(); handler1.As <IDisposable>().Setup(c => c.Dispose()).Verifiable(); var handler2 = new Mock <HttpMessageHandler>(); handler2.As <IDisposable>().Setup(c => c.Dispose()).Verifiable(); var route1 = new Mock <IHttpRoute>(); route1.SetupGet(r => r.Handler).Returns(handler1.Object); var route2 = new Mock <IHttpRoute>(); route2.SetupGet(r => r.Handler).Returns(handler1.Object); var route3 = new Mock <IHttpRoute>(); route3.SetupGet(r => r.Handler).Returns(handler2.Object); collection.Add("route1", route1.Object); collection.Add("route2", route2.Object); collection.Add("route3", route3.Object); // Act collection.Dispose(); // Assert handler1.As <IDisposable>().Verify(c => c.Dispose(), Times.Once()); handler2.As <IDisposable>().Verify(c => c.Dispose(), Times.Once()); }
private static HttpRouteCollection MapHttpAttributeRoutesInternal(this HttpConfiguration configuration, HttpRouteBuilder routeBuilder) { HttpRouteCollection subRoutes = new HttpRouteCollection(); if (configuration == null) { throw Error.ArgumentNull("configuration"); } if (routeBuilder == null) { throw Error.ArgumentNull("routeBuilder"); } List <HttpRouteEntry> attributeRoutes = new List <HttpRouteEntry>(); IHttpControllerSelector controllerSelector = configuration.Services.GetHttpControllerSelector(); IDictionary <string, HttpControllerDescriptor> controllerMap = controllerSelector.GetControllerMapping(); if (controllerMap != null) { foreach (HttpControllerDescriptor controllerDescriptor in controllerMap.Values) { IEnumerable <HttpRouteEntry> controllerRoutes = CreateRouteEntries(controllerDescriptor); foreach (HttpRouteEntry route in controllerRoutes) { route.Route = routeBuilder.BuildHttpRoute(route.Template, route.Actions); } SetDefaultRouteNames(controllerRoutes, controllerDescriptor.ControllerName); attributeRoutes.AddRange(controllerRoutes); } attributeRoutes.Sort(); foreach (HttpRouteEntry attributeRoute in attributeRoutes) { IHttpRoute route = attributeRoute.Route; if (route != null) { subRoutes.Add(attributeRoute.Name, attributeRoute.Route); } } } return(subRoutes); }
public static IHttpRoute MapHttpRoute(this HttpRouteCollection routes, string name, string routeTemplate, object defaults, object constraints, HttpMessageHandler handler, string[] namespaces) { if (routes == null) { throw new ArgumentNullException("routes"); } HttpRouteValueDictionary defaults2 = new HttpRouteValueDictionary(defaults); HttpRouteValueDictionary constraints2 = new HttpRouteValueDictionary(constraints); Dictionary <string, object> dataTokens = new Dictionary <string, object>(); if (namespaces != null && namespaces.Length > 0) { dataTokens.Add("Namespace", namespaces); } IHttpRoute httpRoute = routes.CreateRoute(routeTemplate, defaults2, constraints2, dataTokens, handler); routes.Add(name, httpRoute); return(httpRoute); }