Route

Namespace: Itinero
We found 10 examples in language CSharp for this search. You will see 40 fragments of code.

Other methods

Other methods


        /// <summary>
        /// Gets the precedence or this route.
        /// </summary>
        public static decimal GetPrecedence(this Route route)
        {
            return GetRouteDataTokenValue<decimal>(route, RouteDataTokenKeys.Precedence);
        }

        /// <summary>
        /// Sets the precedence of this Route.
        /// </summary>
        public static void SetPrecedence(this Route route, decimal precedence)
        {
            SetRouteDataTokenValue(route, RouteDataTokenKeys.Precedence, precedence);
        }

        /// <summary>
        /// Gets the order or this route.
        /// </summary>
        public static int GetOrder(this Route route)
        {
            return GetRouteDataTokenValue<int>(route, RouteDataTokenKeys.Order);
        }

        /// <summary>
        /// Sets the order of this Route.
        /// </summary>
        public static void SetOrder(this Route route, int order)
        {
            SetRouteDataTokenValue(route, RouteDataTokenKeys.Order, order);
        }

        /// <summary>
        /// Gets a value indicating whether or not the route is a direct route to an action.
        /// </summary>
        public static bool GetTargetIsAction(this Route route)
        {
            return GetRouteDataTokenValue<bool>(route, RouteDataTokenKeys.TargetIsAction);
        }

        /// <summary>
        /// Sets a value indicating whether or not the route is a direct route to an action.
        /// </summary>
        public static void SetTargetIsAction(this Route route, bool targetIsAction)
        {
            SetRouteDataTokenValue(route, RouteDataTokenKeys.TargetIsAction, targetIsAction);
        }


        [Fact]
        public void IgnoreRouteInternalNeverMatchesUrlGeneration()
        {
            // Arrange
            HttpRouteCollection routes = new HttpRouteCollection();
            IHttpRoute route = routes.IgnoreRoute("Foo", "SomeRouteTemplate");

            // Act
            IHttpVirtualPathData vpd = route.GetVirtualPath(new HttpRequestMessage(HttpMethod.Get, "SomeRouteTemplate"), null);

            // Assert
            Assert.Null(vpd);
        }


        [Fact]
        public void IgnoreRouteInternalNeverMatchesUrlGeneration()
        {
            // Arrange
            RouteCollection routes = new RouteCollection();
            routes.IgnoreRoute("SomeUrl");
            Route route = routes[0] as Route;

            // Act
            VirtualPathData vpd = route.GetVirtualPath(new RequestContext(new Mock<HttpContextBase>().Object, new RouteData()), null);

            // Assert
            Assert.Null(vpd);
        }
            public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
            {
                throw new NotImplementedException();
            }


        /// <inheritdoc/>
        public override void Add(string name, IHttpRoute route)
        {
            _routeCollection.Add(name, route.ToRoute());
        }

        /// <inheritdoc/>
        public override bool Contains(IHttpRoute item)
        {
            foreach (RouteBase route in _routeCollection)
            {
                HttpWebRoute webRoute = route as HttpWebRoute;
                if (webRoute != null && webRoute.HttpRoute == item)
                {
                    return true;
                }
            }

            return false;
        }

        /// <inheritdoc/>
        public override bool TryGetValue(string name, out IHttpRoute route)
        {
            HttpWebRoute rt = _routeCollection[name] as HttpWebRoute;
            if (rt != null)
            {
                route = rt.HttpRoute;
                return true;
            }

            route = null;
            return false;
        }


        [Fact]
        public void Add_WithHostedHttpRoute_RegistersInnerRouteWithAspNetRouteCollection()
        {
            var route = new HostedHttpRoute("uri", null, null, null, null);

            _webApiRoutes.Add("name", route);

            var httpWebRoute = Assert.IsType<HttpWebRoute>(_aspNetRoutes["name"]);
            Assert.Same(route.OriginalRoute, httpWebRoute);
            Assert.Same(route, httpWebRoute.HttpRoute);
        }

        [Fact]
        public void Add_WithNonHostedHttpRoute_WrapsCustomRouteWithHttpWebRoute()
        {
            var route = new Mock<IHttpRoute>().Object;

            _webApiRoutes.Add("name", route);

            var httpWebRoute = Assert.IsType<HttpWebRoute>(_aspNetRoutes["name"]);
            Assert.Same(route, httpWebRoute.HttpRoute);
        }

        [Fact]
        public void Contains_OnlyMatchesRegisteredHttpRouteInstances()
        {
            var route = new Mock<IHttpRoute>().Object;
            _webApiRoutes.Add("bar", route);

            Assert.True(_webApiRoutes.Contains(route));
            Assert.False(_webApiRoutes.Contains(new Mock<IHttpRoute>().Object));
        }

        [Fact]
        public void GetRouteData_WithHttpContext_UnmatchedRoute_ReturnsNull()
        {
            var request = new HttpRequestMessage();
            request.SetHttpContext(CreateHttpContext("~/api2"));
            IHttpRoute route = _webApiRoutes.CreateRoute("api", null, null);
            _webApiRoutes.Add("default", route);

            IHttpRouteData result = _webApiRoutes.GetRouteData(request);

            Assert.Null(result);
        }

        [Fact]
        public void GetRouteData_WithHttpContext_MatchedRoute_ReturnsRouteData()
        {
            var request = new HttpRequestMessage();
            request.SetHttpContext(CreateHttpContext("~/api"));
            IHttpRoute route = _webApiRoutes.CreateRoute("api", null, null);
            _webApiRoutes.Add("default", route);

            IHttpRouteData result = _webApiRoutes.GetRouteData(request);

            Assert.Same(route, result.Route);
        }

        [Fact]
        public void Indexer_ForHttpRoute_ReturnsRoute()
        {
            var route = new Mock<IHttpRoute>().Object;
            _webApiRoutes.Add("foo", route);

            var result = _webApiRoutes["foo"];

            Assert.NotNull(result);
            Assert.Same(route, result);
        }

        [Route("BaseMethodWithRoute")]
        public string BaseMethodWithRoute()
        {
            return "BaseMethodWithRoute";
        }

        [Route("BaseMethodWithRouteWithName", Name = "BaseMethodWithRouteWithName")]
        public string BaseMethodWithRouteWithName()
        {
            return "BaseMethodWithRouteWithName";
        }

        [Route("BaseVirtualMethodWithRoute")]
        public virtual string BaseVirtualMethodWithRoute()
        {
            return "BaseVirtualMethodWithRoute";
        }

        [Route("BaseVirtualMethodWithRouteToBeOverridenWithRoute")]
        public virtual string BaseVirtualMethodWithRouteToBeOverridenWithRoute()
        {
            return "BaseVirtualMethodWithRouteToBeOverridenWithRoute";
        }
        [Route("BaseMethodWithRoute")]
        public string BaseMethodWithRoute()
        {
            return "BaseMethodWithRoute";
        }

        [Route("BaseVirtualMethodWithRoute")]
        public virtual string BaseVirtualMethodWithRoute()
        {
            return "BaseVirtualMethodWithRoute";
        }

            [Route("cool")]
            public void Action1()
            {
            }

            [Route("cool")]
            public void Action2()
            {
            }


        /// <summary>
        /// Builds an <see cref="Route"/> for a particular controller.
        /// </summary>
        /// <param name="routeTemplate">The tokenized route template for the route.</param>
        /// <param name="controllerDescriptor">The controller the route attribute has been applied on.</param>
        /// <returns>The generated <see cref="Route"/>.</returns>
        public Route BuildDirectRoute(string routeTemplate, ControllerDescriptor controllerDescriptor)
        {
            if (routeTemplate == null)
            {
                throw Error.ArgumentNull("routeTemplate");
            }

            if (controllerDescriptor == null)
            {
                throw Error.ArgumentNull("controllerDescriptor");
            }
                        
            string controllerName = controllerDescriptor.ControllerName;
                        
            RouteAreaAttribute area = controllerDescriptor.GetAreaFrom();
            string areaName = controllerDescriptor.GetAreaName(area);

            RouteValueDictionary defaults = new RouteValueDictionary
            {
                { "controller", controllerName }
            };

            Type controllerType = controllerDescriptor.ControllerType;

            RouteValueDictionary dataTokens = new RouteValueDictionary();
            if (areaName != null)
            {
                dataTokens.Add(RouteDataTokenKeys.Area, areaName);
                dataTokens.Add(RouteDataTokenKeys.UseNamespaceFallback, value: false);
                if (controllerType != null)
                {
                    dataTokens.Add(RouteDataTokenKeys.Namespaces, new[] { controllerType.Namespace });
                }
            }

            RouteValueDictionary constraints = new RouteValueDictionary();
            string detokenizedRouteTemplate = InlineRouteTemplateParser.ParseRouteTemplate(routeTemplate, defaults, constraints, ConstraintResolver);

            Route route = new Route(detokenizedRouteTemplate, new MvcRouteHandler())
            {
                Defaults = defaults,
                Constraints = constraints,
                DataTokens = dataTokens
            };

            return route;
        }

        /// <summary>
        /// Builds an <see cref="Route"/> for a particular action.
        /// </summary>
        /// <param name="routeTemplate">The tokenized route template for the route.</param>
        /// <param name="allowedMethods">The HTTP methods supported by the route. A null value specify that all possible methods are supported.</param>
        /// <param name="controllerName">The name of the associated controller.</param>
        /// <param name="actionName">The name of the associated action.</param>
        /// <param name="targetMethod">The method that the route attribute has been applied on.</param>
        /// <param name="areaName"></param>
        /// <returns>The generated <see cref="Route"/>.</returns>
        public Route BuildDirectRoute(string routeTemplate, IEnumerable<string> allowedMethods, string controllerName, string actionName, MethodInfo targetMethod, string areaName)
        {
            if (routeTemplate == null)
            {
                throw Error.ArgumentNull("routeTemplate");
            }

            if (controllerName == null)
            {
                throw Error.ArgumentNull("controllerName");
            }

            if (actionName == null)
            {
                throw Error.ArgumentNull("actionName");
            }

            RouteValueDictionary defaults = new RouteValueDictionary
            {
                { "controller", controllerName },
                { "action", actionName }
            };

            RouteValueDictionary constraints = new RouteValueDictionary();
            if (allowedMethods != null)
            {
                string[] array = allowedMethods.ToArray();
                if (array.Length > 0)
                {
                    // Current method constraint implementation is inefficient since it matches before running the constraint.
                    // Consider checking the HTTP method first in a custom route as a performance optimization.
                    constraints.Add("httpMethod", new HttpMethodConstraint(array));
                }
            }

            RouteValueDictionary dataTokens = new RouteValueDictionary();
            if (areaName != null)
            {
                dataTokens.Add(RouteDataTokenKeys.Area, areaName);
                dataTokens.Add(RouteDataTokenKeys.UseNamespaceFallback, value: false);
                if (targetMethod.DeclaringType != null)
                {
                    dataTokens.Add(RouteDataTokenKeys.Namespaces, new[] { targetMethod.DeclaringType.Namespace });
                }
            }

            string detokenizedRouteTemplate = InlineRouteTemplateParser.ParseRouteTemplate(routeTemplate, defaults, constraints, ConstraintResolver);

            return BuildDirectRoute(defaults, constraints, dataTokens, detokenizedRouteTemplate, targetMethod);
        }

        /// <summary>
        /// Builds an <see cref="Route"/>.
        /// </summary>
        /// <param name="defaults">The route defaults.</param>
        /// <param name="constraints">The route constraints.</param>
        /// <param name="dataTokens"></param>
        /// <param name="routeTemplate">The detokenized route template.</param>
        /// <param name="targetMethod">The method that the route attribute has been applied on.</param>
        /// <returns>The generated <see cref="Route"/>.</returns>
        public virtual Route BuildDirectRoute(RouteValueDictionary defaults, RouteValueDictionary constraints, RouteValueDictionary dataTokens, string routeTemplate, MethodInfo targetMethod)
        {
            Route route = new Route(routeTemplate, new MvcRouteHandler())
            {
                Defaults = defaults,
                Constraints = constraints,
                DataTokens = dataTokens
            };

            return route;
        }

        [Fact]
        public void GetActionDirectRoutes_IfDirectRouteProviderReturnsNull_Throws()
        {
            // Arrange
            var factories = new[] { CreateStubRouteFactory(null) };
            var action = CreateStubActionDescriptor("IgnoreAction");
            var constraintResolver = new Mock<IInlineConstraintResolver>(MockBehavior.Strict).Object;

            var provider = new AccessibleDirectRouteProvider();

            // Act & Assert
            Assert.Throws<InvalidOperationException>(
                () => provider.GetActionDirectRoutes(action, factories, constraintResolver),
                "IDirectRouteFactory.CreateRoute must not return null.");
        }

        [Fact]
        public void GetControllerDirectRoutes_IfDirectRouteProviderReturnsNull_Throws()
        {
            // Arrange
            var factories = new[] { CreateStubRouteFactory(null) };
            var action = CreateStubActionDescriptor("IgnoreAction");
            var constraintResolver = new Mock<IInlineConstraintResolver>(MockBehavior.Strict).Object;

            var provider = new AccessibleDirectRouteProvider();

            // Act & Assert
            Assert.Throws<InvalidOperationException>(
                () => provider.GetControllerDirectRoutes(action.ControllerDescriptor, new[] { action }, factories, constraintResolver),
                "IDirectRouteFactory.CreateRoute must not return null.");
        }

        [Fact]
        public void GetActionDirectRoutes_IfDirectRouteProviderReturnsRouteWithoutActionDescriptors_Throws()
        {
            // Arrange
            IHttpRoute route = new Mock<IHttpRoute>().Object;
            RouteEntry entry = new RouteEntry(name: null, route: route);
            var factories = new[] { CreateStubRouteFactory(entry) };

            var action = CreateStubActionDescriptor("IgnoreAction");
            var constraintResolver = new Mock<IInlineConstraintResolver>(MockBehavior.Strict).Object;

            var provider = new AccessibleDirectRouteProvider();

            // Act & Assert
            string expectedMessage = "The route does not have any associated action descriptors. Routing requires " +
                "that each direct route map to a non-empty set of actions.";
            Assert.Throws<InvalidOperationException>(
                () => provider.GetActionDirectRoutes(action, factories, constraintResolver),
                expectedMessage);
        }

        [Fact]
        public void GetControllerDirectRoutes_IfDirectRouteProviderReturnsRouteWithoutActionDescriptors_Throws()
        {
            // Arrange
            IHttpRoute route = new Mock<IHttpRoute>().Object;
            RouteEntry entry = new RouteEntry(name: null, route: route);
            var factories = new[] { CreateStubRouteFactory(entry) };

            var action = CreateStubActionDescriptor("IgnoreAction");
            var constraintResolver = new Mock<IInlineConstraintResolver>(MockBehavior.Strict).Object;

            var provider = new AccessibleDirectRouteProvider();

            // Act & Assert
            string expectedMessage = "The route does not have any associated action descriptors. Routing requires " +
                "that each direct route map to a non-empty set of actions.";
            Assert.Throws<InvalidOperationException>(
                () => provider.GetControllerDirectRoutes(action.ControllerDescriptor, new[] { action }, factories, constraintResolver),
                expectedMessage);
        }

        [Fact]
        public void GetActionDirectRoutes_IfDirectRouteProviderReturnsRouteWithEmptyActionDescriptors_Throws()
        {
            // Arrange
            HttpRouteValueDictionary dataTokens = new HttpRouteValueDictionary
            {
                { RouteDataTokenKeys.Actions, new HttpActionDescriptor[0] }
            };
            HttpRoute route = new HttpRoute(null, null, null, dataTokens);
            RouteEntry entry = new RouteEntry(name: null, route: route);
            var factories = new[] { CreateStubRouteFactory(entry) };

            var action = CreateStubActionDescriptor("IgnoreAction");
            var constraintResolver = new Mock<IInlineConstraintResolver>(MockBehavior.Strict).Object;

            var provider = new AccessibleDirectRouteProvider();

            // Act & Assert
            string expectedMessage = "The route does not have any associated action descriptors. Routing requires " +
                "that each direct route map to a non-empty set of actions.";
            Assert.Throws<InvalidOperationException>(
                () => provider.GetActionDirectRoutes(action, factories, constraintResolver),
                expectedMessage);
        }

        [Fact]
        public void GetControllerDirectRoute_IfDirectRouteProviderReturnsRouteWithEmptyActionDescriptors_Throws()
        {
            // Arrange
            HttpRouteValueDictionary dataTokens = new HttpRouteValueDictionary
            {
                { RouteDataTokenKeys.Actions, new HttpActionDescriptor[0] }
            };
            HttpRoute route = new HttpRoute(null, null, null, dataTokens);
            RouteEntry entry = new RouteEntry(name: null, route: route);
            var factories = new[] { CreateStubRouteFactory(entry) };

            var action = CreateStubActionDescriptor("IgnoreAction");
            var constraintResolver = new Mock<IInlineConstraintResolver>(MockBehavior.Strict).Object;

            var provider = new AccessibleDirectRouteProvider();

            // Act & Assert
            string expectedMessage = "The route does not have any associated action descriptors. Routing requires " +
                "that each direct route map to a non-empty set of actions.";
            Assert.Throws<InvalidOperationException>(
                () => provider.GetControllerDirectRoutes(action.ControllerDescriptor, new[] { action }, factories, constraintResolver),
                expectedMessage);
        }

        public static RouteCollectionRoute BuildDirectRouteFromMethod<T>(Expression<Action<T>> methodCall)
        {
            SubRouteCollection collector = new SubRouteCollection();
            AddDirectRouteFromMethod(collector, methodCall);
            return new RouteCollectionRoute(collector);
        }

        public static void AddDirectRouteFromMethod<T>(SubRouteCollection collector, Expression<Action<T>> methodCall)
        {
            var method = ((MethodCallExpression)methodCall.Body).Method;
            var attributes = method.GetCustomAttributes(false).OfType<IRouteInfoProvider>();

            var controllerDescriptor = new ReflectedAsyncControllerDescriptor(method.DeclaringType);
            var actionDescriptor = new ReflectedActionDescriptor(method, method.Name, controllerDescriptor);

            foreach (var attribute in attributes)
            {
                var subRoute = new Route(attribute.Template, routeHandler: null);
                subRoute.SetTargetActionDescriptors(new ActionDescriptor[] { actionDescriptor });
                collector.Add(new RouteEntry(null, subRoute));
            }
        }

        public static RouteCollectionRoute BuildDirectRouteFromController<T>()
        {
            SubRouteCollection collector = new SubRouteCollection();
            AddDirectRouteFromController<T>(collector);
            return new RouteCollectionRoute(collector);
        }

        public static void AddDirectRouteFromController<T>(SubRouteCollection collector)
        {
            var controllerType = typeof(T);
            AttributeRoutingMapper.AddRouteEntries(
                collector, 
                new Type[] { controllerType },
                new DefaultInlineConstraintResolver(),
                new DefaultDirectRouteProvider());
        }

        public static void AddDirectRouteMatches(this RouteData routeData, Func<RouteBase, RouteData, bool> selector = null)
        {
            RouteCollectionRoute route = (RouteCollectionRoute)routeData.Route;

            List<RouteData> matches = new List<RouteData>();
            foreach (var subRoute in route)
            {
                RouteData match = new RouteData() { Route = subRoute };
                bool isMatch = selector == null ? true : selector(subRoute, match);
                if (isMatch)
                {
                    matches.Add(match);
                }
            }

            if (matches.Any())
            {
                routeData.SetDirectRouteMatches(matches);
            }
        }

Itinero.Route : IXmlSerializable, IEnumerable

Constructors :

public Route()

Methods :

public Coordinate[] get_Shape()
public Void set_Shape(Coordinate[] value = )
public IAttributeCollection get_Attributes()
public Void set_Attributes(IAttributeCollection value = )
public Stop[] get_Stops()
public Void set_Stops(Stop[] value = )
public Meta[] get_ShapeMeta()
public Void set_ShapeMeta(Meta[] value = )
public Branch[] get_Branches()
public Void set_Branches(Branch[] value = )
public Single get_TotalDistance()
public Void set_TotalDistance(Single value = )
public Single get_TotalTime()
public Void set_TotalTime(Single value = )
public String get_Profile()
public Void set_Profile(String value = )
public IEnumerator<RoutePosition> GetEnumerator()
public Type GetType()
public String ToString()
public Boolean Equals(Object obj = )
public Int32 GetHashCode()