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);
        }
        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;
        }
Exemple #3
0
        public static IHttpRoute MapHttpSessionRoute(this HttpRouteCollection routes,
                                                     string name, string routeTemplate, bool readOnlySession,
                                                     object defaults = null, object constraints = null, HttpMessageHandler handler = null)
        {
            if (routes == null)
            {
                throw new ArgumentNullException(nameof(routes));
            }

            var dataTokens = new HttpRouteValueDictionary();

            if (readOnlySession)
            {
                dataTokens[Constants.EnableReadOnlySessionKey] = true;
            }
            else
            {
                dataTokens[Constants.EnableFullSessionKey] = true;
            }

            var route = routes.CreateRoute(routeTemplate, new HttpRouteValueDictionary(defaults), new HttpRouteValueDictionary(constraints), dataTokens, handler);

            routes.Add(name, route);

            return(route);
        }
        public static IHttpRoute MapHttpRoute(this HttpRouteCollection routes, string name, string routeTemplate, object defaults, object constraints, SessionStateBehavior sessionBehavior)
        {
            var route = routes.CreateRoute(routeTemplate, defaults, constraints);

            route.SetSessionStateBehavior(sessionBehavior);
            routes.Add(name, route);

            return(route);
        }
Exemple #5
0
        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");
            }
            var routeValue = new HttpRouteValueDictionary(new { Namespace = namespaces });//设置路由值
            var route      = routes.CreateRoute(routeTemplate, new HttpRouteValueDictionary(defaults), new HttpRouteValueDictionary(constraints), routeValue, handler);

            routes.Add(name, route);
            return(route);
        }
Exemple #6
0
        public static IHttpRoute MapHttpRoute(this HttpRouteCollection routes, string name, string routeTemplate, object defaults, object constraints, HttpMessageHandler handler, object dataTokens)
        {
            HttpRouteValueDictionary defaultsDictionary    = new HttpRouteValueDictionary(defaults);
            HttpRouteValueDictionary constraintsDictionary = new HttpRouteValueDictionary(constraints);
            HttpRouteValueDictionary dataTokensDictionary  = new HttpRouteValueDictionary(dataTokens);

            routeTemplate = name + "/" + routeTemplate;
            IHttpRoute route = routes.CreateRoute(routeTemplate, defaultsDictionary, constraintsDictionary, dataTokens: dataTokensDictionary, handler: handler);

            routes.Add(name, route);
            return(route);
        }
Exemple #7
0
        /// <summary>映射指定的路由模板并设置默认的路由值、约束和终结点消息处理程序。</summary>
        /// <returns>对映射路由的引用。</returns>
        /// <param name="routes">应用程序的路由的集合。</param>
        /// <param name="name">要映射的路由的名称。</param>
        /// <param name="routeTemplate">路由的路由模板。</param>
        /// <param name="defaults">一个包含默认路由值的对象。</param>
        /// <param name="constraints">一组表达式,用于指定 <paramref name="routeTemplate" /> 的值。</param>
        /// <param name="handler">请求将被调度到的处理程序。</param>
        public static IHttpRoute MapHttpRoute(this HttpRouteCollection routes, string name, string routeTemplate, object defaults, object constraints, HttpMessageHandler handler)
        {
            if (routes == null)
            {
                throw new ArgumentNullException(nameof(routes));
            }
            HttpRouteValueDictionary routeValueDictionary1 = new HttpRouteValueDictionary(defaults);
            HttpRouteValueDictionary routeValueDictionary2 = new HttpRouteValueDictionary(constraints);
            IHttpRoute route = routes.CreateRoute(routeTemplate, (IDictionary <string, object>)routeValueDictionary1, (IDictionary <string, object>)routeValueDictionary2, (IDictionary <string, object>)null, handler);

            routes.Add(name, route);
            return(route);
        }
Exemple #8
0
        public static IHttpRoute MapHttpRoute(this HttpRouteCollection routes, string name, string routeTemplate, object defaults, object constraints, params string[] namespaces)
        {
            HttpRouteValueDictionary     defaultsDictionary    = new HttpRouteValueDictionary(defaults);
            HttpRouteValueDictionary     constraintsDictionary = new HttpRouteValueDictionary(constraints);
            IDictionary <string, object> tokens = new Dictionary <string, object>()
            {
                { "Namespaces", namespaces }
            };
            IHttpRoute route = routes.CreateRoute(routeTemplate, defaultsDictionary, constraintsDictionary, tokens);

            routes.Add(name, route);
            return(route);
        }
Exemple #9
0
        private static IHttpRoute MapHttpRoute(this HttpRouteCollection routes, string name, string routeTemplate, object defaults, object constraints, string[] namespaceTokens)
        {
            HttpRouteValueDictionary     defaultsDictionary    = new HttpRouteValueDictionary(defaults);
            HttpRouteValueDictionary     constraintsDictionary = new HttpRouteValueDictionary(constraints);
            IDictionary <string, object> tokens = new Dictionary <string, object>();

            tokens.Add("Namespaces", namespaceTokens);

            IHttpRoute route = routes.CreateRoute(routeTemplate, defaultsDictionary, constraintsDictionary, dataTokens: tokens, handler: null);

            routes.Add(name, route);

            return(route);
        }
        public static IHttpRoute MapODataServiceBatchRoute(this HttpRouteCollection routes, string name,
                                                           string routeTemplate,
                                                           object defaults, object constraints,
                                                           HttpMessageHandler handler)
        {
            var route = routes.CreateRoute(routeTemplate, new HttpRouteValueDictionary(defaults),
                                           new HttpRouteValueDictionary(constraints),
                                           new HttpRouteValueDictionary()
            {
                { "RouteId", name }
            },
                                           handler);

            routes.Add(name, route);
            return(route);
        }
Exemple #11
0
        static void ApiRoute(HttpRouteCollection routes, string url, [AspMvcController] string controller, [AspMvcAction] string action)
        {
            url = String.Format("{0}/{1}", ConstantStrings.WebApiExecutionPath, url);
            HttpRouteValueDictionary defaults = new HttpRouteValueDictionary();

            if (controller != null)
            {
                defaults["controller"] = controller;
            }
            if (action != null)
            {
                defaults["action"] = action;
            }
            IHttpRoute route = routes.CreateRoute(url, defaults, new Dictionary <string, object>());

            routes.Add(route.RouteTemplate, route);
        }
Exemple #12
0
        public IHttpRoute AddRoute(string routeName, string routeTemplate, IEnumerable <HttpMethod> methods, HttpRouteCollection routes)
        {
            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()));
            }
            var httpRoute = routes.CreateRoute(routeBuilder.Template, routeBuilder.Defaults, constraints);

            routes.Add(routeName, httpRoute);

            return(httpRoute);
        }
Exemple #13
0
        public static void ApiRoute(HttpRouteCollection routes, string url, [AspMvcController] string controller, [AspMvcAction] string action, bool sessionRequired = false)
        {
            url = String.Format("{0}/{1}", ConstantStrings.WebApiExecutionPath, url);
            HttpRouteValueDictionary defaults = new HttpRouteValueDictionary();

            if (controller != null)
            {
                defaults["controller"] = controller;
            }
            if (action != null)
            {
                defaults["action"] = action;
            }
            IHttpRoute route = routes.CreateRoute(url, defaults, new Dictionary <string, object>());

            routes.Add(route.RouteTemplate, route);
            if (sessionRequired)
            {
                // в урле передаётся название параметра. превращаем его в регулярку.
                var urlRegex = $"^~/" + Regex.Replace(url, @"{.+?}", "(.+?)");
                ActionSessionHelper.RegisterUrlRegex(urlRegex);
            }
        }
        public void CreateRoute_ValidatesConstraintType_InvalidType()
        {
            // Arrange
            var routes = new HttpRouteCollection();

            var constraint = new Uri("http://localhost/");
            var constraints = new HttpRouteValueDictionary();
            constraints.Add("custom", constraint);

            string expectedMessage =
                "The constraint entry 'custom' on the route with route template '{controller}/{id}' " +
                "must have a string value or be of a type which implements 'System.Web.Http.Routing.IHttpRouteConstraint'.";

            // Act & Assert
            Assert.Throws<InvalidOperationException>(() => routes.CreateRoute("{controller}/{id}", null, constraints), expectedMessage);
        }