Esempio n. 1
0
        public IEnumerable<AggregatedRoute> GetAggregatedRoutes(MethodInfo method)
        {
            if (method.DeclaringType == null) yield break;

            var routePrefixes = GetRoutePrefixes(method);

            var routeAttrs = method.GetCustomAttributes(typeof (RouteAttribute), false);

            AggregatedRoute defaultRoute = null;
            var defaultRouteAdded = false;

            foreach (RouteAttribute routePathAttr in routeAttrs)
            {
                if (defaultRouteAdded && routePathAttr.Name == null)
                {
                    throw new ArgumentException(string.Format(
                        "Multiple routes defined without route names in {0} for {1}.",
                        method.DeclaringType.FullName,
                        method.Name));
                }

                var currentRoute = new AggregatedRoute {Path = routePathAttr.Path};

                // Add the single unnamed route as default route.
                if (!defaultRouteAdded && routePathAttr.Name == null)
                {
                    defaultRoute = currentRoute;
                    defaultRouteAdded = true;
                }

                // Resolve RouteMethodAttribute.

                var routeMethodAttrs = method.GetCustomAttributes(typeof (RouteMethodAttribute), false);
                {
                    AggregatedRoute route = null;
                    var setRouteMethod = false;

                    if (!routeMethodAttrs.Any())
                    {
                        currentRoute.RequestMethods.Add("GET");
                    }
                    foreach (RouteMethodAttribute item in routeMethodAttrs)
                    {
                        if (item.RouteName == null && defaultRouteAdded)
                        {
                            // Route unnamed. Use default route.
                            route = defaultRoute;
                            setRouteMethod = true;
                        }
                        else if (item.RouteName == routePathAttr.Name)
                        {
                            // Route is explicity named. Use the current route.
                            route = currentRoute;
                            setRouteMethod = true;
                        }

                        if (setRouteMethod)
                        {
                            var methods = item.RouteMethod.Where(m => !route.RequestMethods.Contains(m));
                            foreach (var m in methods)
                            {
                                route.RequestMethods.Add(m);
                            }
                        }
                    }
                }

                foreach (var routePrefix in routePrefixes)
                {
                    var newRoute = new AggregatedRoute
                    {
                        ActionMethodInfo = method,
                        RequestMethods = currentRoute.RequestMethods,
                        Priority = routePathAttr.Priority,
                        Path = StringHelpers.CombineWebPath(routePrefix, currentRoute.Path),
                        Handlers = GetRouteHandlerCollection(method),
                        RequestModel = GetRouteRequestModel(method)
                    };

                    yield return newRoute;
                }
            }
        }
Esempio n. 2
0
        public IEnumerable <AggregatedRoute> GetAggregatedRoutes(MethodInfo method)
        {
            if (method.DeclaringType == null)
            {
                yield break;
            }

            var routePrefixes = GetRoutePrefixes(method);

            var routeAttrs = method.GetCustomAttributes(typeof(RouteAttribute), false);

            AggregatedRoute defaultRoute      = null;
            var             defaultRouteAdded = false;

            foreach (RouteAttribute routePathAttr in routeAttrs)
            {
                if (defaultRouteAdded && routePathAttr.Name == null)
                {
                    throw new ArgumentException(string.Format(
                                                    "Multiple routes defined without route names in {0} for {1}.",
                                                    method.DeclaringType.FullName,
                                                    method.Name));
                }

                var currentRoute = new AggregatedRoute {
                    Path = routePathAttr.Path
                };

                // Add the single unnamed route as default route.
                if (!defaultRouteAdded && routePathAttr.Name == null)
                {
                    defaultRoute      = currentRoute;
                    defaultRouteAdded = true;
                }

                // Resolve RouteMethodAttribute.

                var routeMethodAttrs = method.GetCustomAttributes(typeof(RouteMethodAttribute), false);
                {
                    AggregatedRoute route          = null;
                    var             setRouteMethod = false;

                    if (!routeMethodAttrs.Any())
                    {
                        currentRoute.RequestMethods.Add("GET");
                    }
                    foreach (RouteMethodAttribute item in routeMethodAttrs)
                    {
                        if (item.RouteName == null && defaultRouteAdded)
                        {
                            // Route unnamed. Use default route.
                            route          = defaultRoute;
                            setRouteMethod = true;
                        }
                        else if (item.RouteName == routePathAttr.Name)
                        {
                            // Route is explicity named. Use the current route.
                            route          = currentRoute;
                            setRouteMethod = true;
                        }

                        if (setRouteMethod)
                        {
                            var methods = item.RouteMethod.Where(m => !route.RequestMethods.Contains(m));
                            foreach (var m in methods)
                            {
                                route.RequestMethods.Add(m);
                            }
                        }
                    }
                }

                foreach (var routePrefix in routePrefixes)
                {
                    var newRoute = new AggregatedRoute
                    {
                        ActionMethodInfo = method,
                        RequestMethods   = currentRoute.RequestMethods,
                        Priority         = routePathAttr.Priority,
                        Path             = StringHelpers.CombineWebPath(routePrefix, currentRoute.Path),
                        Handlers         = GetRouteHandlerCollection(method),
                        RequestModel     = GetRouteRequestModel(method)
                    };

                    yield return(newRoute);
                }
            }
        }