IEnumerable <LocatedControllerAction> IControllerActionLocator.Where(Func <Type, bool> predicate)
        {
            var actionTypes = from type in types
                              where type.IsPublic
                              where !type.IsAbstract
                              where !type.IsInterface
                              where predicate(type)
                              select new LocatedControllerAction(type, namingConventions.BuildKeyFromType(type));

            return(actionTypes);
        }
        public Dictionary <string, Type> Build()
        {
            var actionTypes = (from type in types
                               where type.IsPublic
                               where type != typeof(ActionController)
                               where typeof(ActionController).IsAssignableFrom(type)
                               where !type.IsAbstract
                               where !type.IsInterface
                               select type);

            var located = actionTypes
                          .Where(x => predicates.All(func => func(x)))
                          .Select(x => new { Type = x, Name = namingConventions.BuildKeyFromType(x) }).ToDictionary(x => x.Name, x => x.Type);

            return(located);
        }
Beispiel #3
0
        public Dictionary <string, Type> Build()
        {
            var actionTypes = (from type in types
                               where type.IsPublic
                               where type != typeof(ActionController)
                               where typeof(ActionController).IsAssignableFrom(type)
                               where !type.IsAbstract
                               where !type.IsInterface
                               select type);

            var located = actionTypes
                          .Where(x => x.GetCustomAttributes(typeof(RouteAttribute), false).Any())
                          .Where(x => predicates.All(func => func(x)))
                          .Select(x =>
            {
                var type       = x;
                var key        = namingConventions.BuildKeyFromType(x);
                var controller = namingConventions.BuildControllerFromType(x);
                var action     = namingConventions.BuildActionFromType(x);
                var routeAttr  = x.GetCustomAttributes(typeof(RouteAttribute), false).OfType <RouteAttribute>().First();

                // Set Defautls
                var defaults = ParseRouteValues(routeAttr.Defaults);
                defaults.Add("controller", controller);
                defaults.Add("action", action);

                // Set Optional Parameters and remove '?' mark from the url
                Match m;
                while ((m = Regex.Match(routeAttr.Url, @"\{([^\}]+?)\?\}")) != null && m.Success)
                {
                    var p = m.Groups[1].Value;
                    defaults.Add(p, UrlParameter.Optional);
                    routeAttr.Url = routeAttr.Url.Replace("{" + p + "?}", "{" + p + "}");
                }

                // Set Defautls
                var constraints = ParseRouteValues(routeAttr.Constraints);

                // Set Data Tokens
                var dataTokens = new RouteValueDictionary();
                dataTokens.Add("Namespaces", new[] { type.Namespace });

                //Setup Actions
                var methods = type.GetMethods().Where(y => y.Name == "Get" || y.Name == "Post" || y.Name == "Execute");
                foreach (var meth in methods)
                {
                    var par = meth.GetParameters().FirstOrDefault();
                    if (par == null)
                    {
                        throw new Exception("No Parameter found for action - " + meth.DeclaringType + ", " + meth.Name);
                    }

                    ActionFactory.Actions.Add(par.ParameterType, new ActionInfo()
                    {
                        ClassType  = meth.DeclaringType,
                        HttpMethod = meth.Name,
                        Name       = meth.Name,
                        Controller = controller,
                        Action     = action
                    });

                    var route = new Route(routeAttr.Url.TrimStart('/'), new MvcRouteHandler())
                    {
                        Defaults    = defaults,
                        Constraints = constraints,
                        DataTokens  = dataTokens
                    };

                    RouteTable.Routes.Add(par.ParameterType.FullName, route);
                }

                return(new { key, type });
            }).ToDictionary(x => x.key, x => x.type);

            return(located);
        }