Microsoft.Azure.WebJobs.Script.Binding.HttpRouteFactory.TryAddRoute C# (CSharp) Method

TryAddRoute() public method

public TryAddRoute ( string routeName, string routeTemplate, IEnumerable methods, HttpRouteCollection routes, IHttpRoute &route ) : bool
routeName string
routeTemplate string
methods IEnumerable
routes HttpRouteCollection
route IHttpRoute
return bool
        public bool TryAddRoute(string routeName, string routeTemplate, IEnumerable<HttpMethod> methods, HttpRouteCollection routes, out IHttpRoute route)
        {
            route = null;

            try
            {
                var routeBuilder = CreateRouteBuilder(routeTemplate);
                var constraints = routeBuilder.Constraints;
                if (methods != null)
                {
                    // if the methods collection is not null, apply the constraint
                    // if the methods collection is empty, we'll create a constraint
                    // that disallows ALL methods
                    constraints.Add("httpMethod", new HttpMethodConstraint(methods.ToArray()));
                }
                route = routes.CreateRoute(routeBuilder.Template, routeBuilder.Defaults, constraints);
                routes.Add(routeName, route);
            }
            catch (Exception ex) when (!ex.IsFatal()) 
            {
                // catch any route parsing errors
                return false;
            }

            return true;
        }

Usage Example

        public static void TryAddRoute_AmbiguousRoute_FirstRouteIsChosen()
        {
            HttpRouteFactory routeFactory = new HttpRouteFactory("api");

            HttpRouteCollection routes = new HttpRouteCollection();
            IHttpRoute route1, route2;
            Assert.True(routeFactory.TryAddRoute("route1", "foo/bar/baz", null, routes, out route1));
            Assert.True(routeFactory.TryAddRoute("route2", "foo/bar/baz", null, routes, out route2));

            HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://host/api/foo/bar/baz");
            var routeData = routes.GetRouteData(request);
            Assert.Same(route1, routeData.Route);
        }
All Usage Examples Of Microsoft.Azure.WebJobs.Script.Binding.HttpRouteFactory::TryAddRoute