private static ActionDescriptor CreateActionDescriptor(ControllerDescriptor controller,
            AsyncActionMethodSelector actionSelector, MethodInfo method)
        {
            string actionName = actionSelector.GetActionName(method);
            ActionDescriptorCreator creator = actionSelector.GetActionDescriptorDelegate(method);
            Debug.Assert(creator != null);

            return creator(actionName, controller);
        }
Exemple #2
0
        private static List <ActionDescriptor> GetActionDescriptors(ReflectedAsyncControllerDescriptor controller)
        {
            Contract.Assert(controller != null);

            AsyncActionMethodSelector actionSelector = controller.Selector;

            var actions = new List <ActionDescriptor>();

            foreach (MethodInfo method in actionSelector.ActionMethods)
            {
                string actionName = actionSelector.GetActionName(method);
                ActionDescriptorCreator creator = actionSelector.GetActionDescriptorDelegate(method);
                Debug.Assert(creator != null);

                ActionDescriptor action = creator(actionName, controller);
                actions.Add(action);
            }

            return(actions);
        }
        internal List <RouteEntry> MapMvcAttributeRoutes(ReflectedAsyncControllerDescriptor controllerDescriptor)
        {
            string prefix = controllerDescriptor.GetPrefixFrom();

            ValidatePrefixTemplate(prefix, controllerDescriptor);

            RouteAreaAttribute area       = controllerDescriptor.GetAreaFrom();
            string             areaName   = controllerDescriptor.GetAreaName(area);
            string             areaPrefix = area != null ? area.AreaPrefix ?? area.AreaName : null;

            ValidateAreaPrefixTemplate(areaPrefix, areaName, controllerDescriptor);

            AsyncActionMethodSelector actionSelector    = controllerDescriptor.Selector;
            IEnumerable <MethodInfo>  actionMethodsInfo = actionSelector.DirectRouteMethods;

            List <RouteEntry> routeEntries = new List <RouteEntry>();

            foreach (var method in actionMethodsInfo)
            {
                string actionName = actionSelector.GetActionName(method);
                ActionDescriptorCreator creator = actionSelector.GetActionDescriptorDelegate(method);
                Debug.Assert(creator != null);

                ActionDescriptor actionDescriptor = creator(actionName, controllerDescriptor);

                IEnumerable <IRouteInfoProvider> routeAttributes = GetRouteAttributes(method, controllerDescriptor.ControllerType);

                foreach (var routeAttribute in routeAttributes)
                {
                    ValidateTemplate(routeAttribute.Template, actionName, controllerDescriptor);

                    string template = CombinePrefixAndAreaWithTemplate(areaPrefix, prefix, routeAttribute.Template);
                    Route  route    = _routeBuilder.BuildDirectRoute(template, routeAttribute, controllerDescriptor, actionDescriptor);

                    RouteEntry entry = new RouteEntry
                    {
                        Name     = routeAttribute.Name,
                        Route    = route,
                        Template = template,
                    };

                    routeEntries.Add(entry);
                }
            }

            // Check for controller-level routes.
            IEnumerable <IRouteInfoProvider> controllerRouteAttributes = controllerDescriptor.GetDirectRoutes();

            List <ActionDescriptor> actions = new List <ActionDescriptor>();

            foreach (var actionMethod in actionSelector.StandardRouteMethods)
            {
                string actionName = actionSelector.GetActionName(actionMethod);
                ActionDescriptorCreator creator = actionSelector.GetActionDescriptorDelegate(actionMethod);
                Debug.Assert(creator != null);

                ActionDescriptor actionDescriptor = creator(actionName, controllerDescriptor);
                actions.Add(actionDescriptor);
            }

            // Don't create a route for the controller-level attributes if no actions could possibly match
            if (actions.Any())
            {
                foreach (var routeAttribute in controllerRouteAttributes)
                {
                    string template = CombinePrefixAndAreaWithTemplate(areaPrefix, prefix, routeAttribute.Template);

                    Route      route = _routeBuilder.BuildDirectRoute(template, routeAttribute, controllerDescriptor, actions);
                    RouteEntry entry = new RouteEntry
                    {
                        Name     = routeAttribute.Name,
                        Route    = route,
                        Template = template,
                    };

                    routeEntries.Add(entry);
                }
            }

            return(routeEntries);
        }