コード例 #1
0
        public static void Add <T>(this RoutingEngine engine)
            where T : class, IController
        {
            string name = typeof(T).Name.Replace("Controller", string.Empty);
            var    controllerDetails =
                typeof(T).GetCustomAttributes(typeof(ControllerDetailsAttribute), true).FirstOrDefault() as
                ControllerDetailsAttribute;
            string area = string.Empty;

            if (controllerDetails != null)
            {
                area = controllerDetails.Area;
            }

            PatternRoute patternIdRoute =
                new PatternRoute(string.Format("{1}/{0}/<id>/<action>", name,
                                               string.IsNullOrEmpty(area) ? string.Empty : string.Format("/{0}", area)))
                .DefaultForController().Is <T>()
                .Restrict("id").ValidInteger
                .DefaultForAction().Is("index");

            PatternRoute patternRoute =
                new PatternRoute(string.Format("{1}/{0}/<action>", name,
                                               string.IsNullOrEmpty(area) ? string.Empty : string.Format("/{0}", area)))
                .DefaultForController().Is <T>()
                .DefaultForAction().Is("index");

            if (!string.IsNullOrEmpty(area))
            {
                patternIdRoute.DefaultForArea().Is(area);
                patternRoute.DefaultForArea().Is(area);
            }
            engine.Add(patternIdRoute);
            engine.Add(patternRoute);
        }