コード例 #1
0
        /// <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);
        }
        /// <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);
        }
コード例 #3
0
        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);
        }
コード例 #4
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");
            }
            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);
        }