예제 #1
0
        public void GetRouteData_IfHttpRouteGetRouteDataThrows_RoutesToExceptionHandler()
        {
            // Arrange
            Exception  expectedException = CreateException();
            IHttpRoute route             = CreateThrowingRoute(expectedException);

            HttpWebRoute product = CreateProductUnderTest(route);

            HttpRequestBase requestBase = CreateStubRequestBase("GET");
            HttpContextBase contextBase = CreateStubContextBase(requestBase);

            using (HttpRequestMessage request = CreateRequest())
            {
                contextBase.SetHttpRequestMessage(request);

                // Act
                RouteData routeData = product.GetRouteData(contextBase);

                // Assert
                Assert.NotNull(routeData);
                Assert.Same(product, routeData.Route);
                HttpRouteExceptionRouteHandler typedHandler =
                    Assert.IsType <HttpRouteExceptionRouteHandler>(routeData.RouteHandler);
                ExceptionDispatchInfo exceptionInfo = typedHandler.ExceptionInfo;
                Assert.NotNull(exceptionInfo); // Guard
                Assert.Same(expectedException, exceptionInfo.SourceException);
            }
        }
예제 #2
0
        public HostedHttpRoute(
            string uriTemplate,
            IDictionary <string, object> defaults,
            IDictionary <string, object> constraints,
            IDictionary <string, object> dataTokens,
            HttpMessageHandler handler
            )
        {
            RouteValueDictionary routeDefaults =
                defaults != null ? new RouteValueDictionary(defaults) : null;
            RouteValueDictionary routeConstraints =
                constraints != null ? new RouteValueDictionary(constraints) : null;
            RouteValueDictionary routeDataTokens =
                dataTokens != null ? new RouteValueDictionary(dataTokens) : null;

            OriginalRoute = new HttpWebRoute(
                uriTemplate,
                routeDefaults,
                routeConstraints,
                routeDataTokens,
                HttpControllerRouteHandler.Instance,
                this
                );
            Handler = handler;
        }
예제 #3
0
 public HostedHttpRoute(string uriTemplate, IDictionary<string, object> defaults, IDictionary<string, object> constraints, IDictionary<string, object> dataTokens, HttpMessageHandler handler)
 {
     RouteValueDictionary routeDefaults = defaults != null ? new RouteValueDictionary(defaults) : null;
     RouteValueDictionary routeConstraints = constraints != null ? new RouteValueDictionary(constraints) : null;
     RouteValueDictionary routeDataTokens = dataTokens != null ? new RouteValueDictionary(dataTokens) : null;
     OriginalRoute = new HttpWebRoute(uriTemplate, routeDefaults, routeConstraints, routeDataTokens, HttpControllerRouteHandler.Instance, this);
     Handler = handler;
 }
예제 #4
0
 /// <inheritdoc/>
 protected override void ValidateConstraint(
     string routeTemplate,
     string name,
     object constraint
     )
 {
     // In WebHost the constraint might be IHttpRouteConstraint or IRouteConstraint (System.Web) or a string
     HttpWebRoute.ValidateConstraint(routeTemplate, name, constraint);
 }
        public override IHttpRoute CreateRoute(string uriTemplate, IDictionary <string, object> defaults, IDictionary <string, object> constraints, IDictionary <string, object> dataTokens, IDictionary <string, object> parameters)
        {
            RouteValueDictionary routeDefaults    = defaults != null ? new RouteValueDictionary(defaults) : null;
            RouteValueDictionary routeConstraints = constraints != null ? new RouteValueDictionary(constraints) : null;
            RouteValueDictionary routeDataTokens  = dataTokens != null ? new RouteValueDictionary(dataTokens) : null;
            HttpWebRoute         route            = new HttpWebRoute(uriTemplate, routeDefaults, routeConstraints, routeDataTokens, HttpControllerRouteHandler.Instance);

            return(new HostedHttpRoute(route));
        }
        /// <inheritdoc/>
        public override IHttpRoute this[int index]
        {
            get
            {
                HttpWebRoute route = _routeCollection[index] as HttpWebRoute;
                if (route != null)
                {
                    return(route.HttpRoute);
                }

                throw Error.ArgumentOutOfRange("index", index, SRResources.RouteCollectionOutOfRange);
            }
        }
        /// <inheritdoc/>
        public override IHttpRoute this[string name]
        {
            get
            {
                HttpWebRoute route = _routeCollection[name] as HttpWebRoute;
                if (route != null)
                {
                    return(route.HttpRoute);
                }

                throw Error.KeyNotFound();
            }
        }
        /// <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);
        }
        /// <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);
        }
예제 #10
0
        public HostedHttpRouteData(RouteData routeData)
        {
            if (routeData == null)
            {
                throw Error.ArgumentNull("routeData");
            }

            OriginalRouteData = routeData;

            HttpWebRoute route = routeData.Route as HttpWebRoute;

            Route = route == null ? null : route.HttpRoute;
        }
예제 #11
0
        public HostedHttpRoute(string uriTemplate, IDictionary <string, object> defaults, IDictionary <string, object> constraints, IDictionary <string, object> dataTokens, HttpMessageHandler handler)
        {
            RouteValueDictionary routeDefaults    = defaults != null ? new RouteValueDictionary(defaults) : null;
            RouteValueDictionary routeConstraints = constraints != null ? new RouteValueDictionary(constraints) : null;
            RouteValueDictionary routeDataTokens  = dataTokens != null ? new RouteValueDictionary(dataTokens) : null;

            /*
             * 实现在HostedHttpRoute之中的核心路由功能基本上是通过这个Route对象完成的,
             * 所以我们才说ASP.NET Web API的路由在Web Host寄宿模式下还是利用ASP.NET自身的路由系统实现的
             *
             * **/
            OriginalRoute = new HttpWebRoute(uriTemplate, routeDefaults, routeConstraints, routeDataTokens, HttpControllerRouteHandler.Instance, this);


            Handler = handler;
        }
        /// <summary>
        /// Maps the specified route template and sets default route values, constraints, and namespaces.
        /// </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 Route MapHttpRoute(this RouteCollection routes, string name, string routeTemplate, object defaults, object constraints)
        {
            if (routes == null)
            {
                throw Error.ArgumentNull("routes");
            }

            HttpWebRoute route = new HttpWebRoute(routeTemplate, HttpControllerRouteHandler.Instance)
            {
                Defaults = new RouteValueDictionary(defaults),
                Constraints = new RouteValueDictionary(constraints),
                DataTokens = new RouteValueDictionary()
            };

            routes.Add(name, route);
            return route;
        }
 public override IHttpRoute CreateRoute(string uriTemplate, IDictionary<string, object> defaults, IDictionary<string, object> constraints, IDictionary<string, object> dataTokens, IDictionary<string, object> parameters)
 {
     RouteValueDictionary routeDefaults = defaults != null ? new RouteValueDictionary(defaults) : null;
     RouteValueDictionary routeConstraints = constraints != null ? new RouteValueDictionary(constraints) : null;
     RouteValueDictionary routeDataTokens = dataTokens != null ? new RouteValueDictionary(dataTokens) : null;
     HttpWebRoute route = new HttpWebRoute(uriTemplate, routeDefaults, routeConstraints, routeDataTokens, HttpControllerRouteHandler.Instance);
     return new HostedHttpRoute(route);
 }
        /// <summary>
        /// Builds the contents for the route table based on the attributes
        /// found on the method of a specific controller.
        /// </summary>
        /// <param name="routes"></param>
        /// <param name="controllerType"></param>
        /// <param name="method"></param>
        private static void BuildControllerMethodRoutes(RouteCollection routes, Type controllerType, MethodInfo method)
        {
            // Grab the http route attributes from the current method.
            HttpRouteAttribute[] attributes = (HttpRouteAttribute[])method.GetCustomAttributes(
                typeof(HttpRouteAttribute), true);

            if (attributes.Length != 0)
            {
                // Automatically grab the controller name and action name
                // from the method and controller type.
                string action = method.Name;
                string controller = controllerType.Name;

                // Translate the somewhat weird controller name into one the routing system
                // understands, by removing the Controller part from the name.
                if (controller.EndsWith("Controller", StringComparison.Ordinal))
                {
                    controller = controller.Substring(0, controller.IndexOf("Controller"));
                }

                // Generate a route for every HTTP route attribute found on the method
                foreach (var attribute in attributes)
                {
                    var routeValueDictionary = new RouteValueDictionary();

                    routeValueDictionary.Add("controller", controller);
                    routeValueDictionary.Add("action", action);

                    ResolveOptionalRouteParameters(attribute.UriTemplate, method, routeValueDictionary);

                    // Create the route and attach the default route handler to it.
                    HttpWebRoute route = new HttpWebRoute(attribute.UriTemplate, routeValueDictionary,
                        new RouteValueDictionary(), new RouteValueDictionary(), HttpControllerRouteHandler.Instance);

                    routes.Add(Guid.NewGuid().ToString(), route);
                }
            }
        }
        /// <summary>
        /// Builds the contents for the route table based on the attributes
        /// found on a specific controller type.
        /// </summary>
        /// <param name="routes"></param>
        /// <param name="controllerType"></param>
        private static void BuildControllerRoutes(RouteCollection routes, Type controllerType)
        {
            HttpRouteAttribute[] attributes = (HttpRouteAttribute[])controllerType.GetCustomAttributes(
                typeof(HttpRouteAttribute), true);

            string controller = controllerType.Name;

            // Translate the somewhat weird controller name into one the routing system
            // understands, by removing the Controller part from the name.
            if (controller.EndsWith("Controller", StringComparison.Ordinal))
            {
                controller = controller.Substring(0, controller.IndexOf("Controller"));
            }

            foreach (var attribute in attributes)
            {
                RouteValueDictionary routeValuesDictionary = new RouteValueDictionary();
                routeValuesDictionary.Add("controller", controller);

                // Create the route and attach the default route handler to it.
                HttpWebRoute route = new HttpWebRoute(attribute.UriTemplate, routeValuesDictionary,
                    new RouteValueDictionary(), new RouteValueDictionary(), HttpControllerRouteHandler.Instance);

                routes.Add(Guid.NewGuid().ToString(), route);
            }
        }