示例#1
0
        /// <summary>
        /// Adds all <see cref="Controller"/> types found in the given assembly that
        /// are annotated with a <see cref="PrefixAttribute"/>.
        /// </summary>
        /// <param name="assembly">Assembly to search for <see cref="Controller"/> types.</param>
        public void Add(Assembly assembly)
        {
            foreach (var type in assembly.GetTypes())
            {
                if (!typeof(Controller).IsAssignableFrom(type))
                {
                    continue;
                }

                var attribs = type.GetCustomAttributes <PrefixAttribute>().AsArray();
                if (attribs.Length == 0)
                {
                    continue;
                }

                var ctor = type.GetConstructor(Type.EmptyTypes);
                if (ctor == null)
                {
                    continue;
                }

                var ctorCall = Expression.New(ctor);
                var lambda   = Expression.Lambda <Func <Controller> >(ctorCall).Compile();

                foreach (var attrib in attribs)
                {
                    var matcher = UrlMatcher.Parse(attrib.Value, attrib.Extension);
                    Add(matcher, attrib.Priority, type, lambda);
                }
            }
        }
示例#2
0
        private ControllerActionMap(Type controllerType)
        {
            ControllerType = controllerType;

            foreach (var method in ControllerType.GetMethods(InstanceFlags))
            {
                var attribs = method.GetCustomAttributes <ControllerActionAttribute>(true).AsArray();

                foreach (var attrib in attribs)
                {
                    var action     = GenerateAction(method, attrib);
                    var matcher    = UrlMatcher.Parse(attrib.Value, attrib.Extension);
                    var httpMethod = attrib is GetAttribute ? HttpMethod.Get
                        : attrib is PostAttribute ? HttpMethod.Post : null;

                    _actions.Add(new BoundAction(matcher, attrib.MatchAllUrl,
                                                 attrib.Priority, httpMethod, action, method.Name));
                }
            }

            _actions.Sort();
        }