Example #1
0
        /// <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);
            }
        }
        /// <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);
        }
Example #3
0
        /// <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);
                }
            }
        }