RouteTableRegister.cs :  » Content-Management-Systems-CMS » Kooboo » Everest » Library » Mvc » Routing » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Content Management Systems CMS » Kooboo 
Kooboo » Everest » Library » Mvc » Routing » RouteTableRegister.cs
/*
Kooboo is a content management system based on ASP.NET MVC framework. Copyright 2009 Yardi Technology Limited.

This program is free software: you can redistribute it and/or modify it under the terms of the
GNU General Public License version 3 as published by the Free Software Foundation.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program.
If not, see http://www.kooboo.com/gpl3/.
*/
using System;
using System.Web.Mvc;
using System.Web.Routing;
using System.Collections.Generic;
using System.Configuration;
using Everest.Library.Configuration;

namespace Everest.Library.Mvc.Routing{
    public class RouteTableRegister
    {
        public static void RegisterRoutes(RouteCollection routes, string routeFile)
        {
            lock (routes)
            {
                RouteTableSection routesTableSection = GetRouteTableConfigurationSection(routeFile);
                if (routesTableSection != null)
                {
                    //ignore route
                    if (routesTableSection.Ignores.Count > 0)
                    {
                        foreach (ConfigurationElement item in routesTableSection.Ignores)
                        {
                            routes.IgnoreRoute(((IgnoreRouteElement)item).Url, GetDictionaryFromAttributes(((IgnoreRouteElement)item).Constraints.Attributes));
                        }
                    }

                    if (routesTableSection.Routes.Count > 0)
                    {
                        for (int routeIndex = 0; routeIndex < routesTableSection.Routes.Count; routeIndex++)
                        {
                            RouteConfigElement route = routesTableSection.Routes[routeIndex] as RouteConfigElement;
                            if (routes[route.Name] == null)
                            {
                                if (string.IsNullOrEmpty(route.RouteType))
                                {
                                    routes.Add(route.Name, new Route(
                                                        route.Url,
                                                        GetDefaults(route),
                                                        GetConstraints(route),
                                                        GetDataTokens(route),
                                                        GetInstanceOfRouteHandler(route)));
                                }
                                else
                                {
                                    var customRoute = (RouteBase)Activator.CreateInstance(Type.GetType(route.RouteType),
                                                            route.Url,
                                                            GetDefaults(route),
                                                            GetConstraints(route),
                                                            GetDataTokens(route),
                                                            GetInstanceOfRouteHandler(route));
                                    routes.Add(route.Name, customRoute);
                                }
                            }
                        }
                    }
                }
            }
        }


        /// <summary>
        /// Gets the route table configuration section.
        /// </summary>
        /// <param name="fileName">Name of the file.</param>
        /// <returns></returns>
        private static RouteTableSection GetRouteTableConfigurationSection(string fileName)
        {
            return (RouteTableSection)ConfigurationSectionManager.GetSection(typeof(RouteTableSection), fileName);
        }

        /// <summary>
        /// Gets the instance of route handler.
        /// </summary>
        /// <param name="route">The route.</param>
        /// <returns></returns>
        private static IRouteHandler GetInstanceOfRouteHandler(RouteConfigElement route)
        {
            IRouteHandler routeHandler;

            if (string.IsNullOrEmpty(route.RouteHandlerType))
                routeHandler = new MvcRouteHandler();
            else
            {
                try
                {
                    Type routeHandlerType = Type.GetType(route.RouteHandlerType);
                    routeHandler = Activator.CreateInstance(routeHandlerType) as IRouteHandler;
                }
                catch (Exception e)
                {
                    throw new ApplicationException(
                                 string.Format("Can't create an instance of IRouteHandler {0}", route.RouteHandlerType),
                                 e);
                }

            }

            return routeHandler;
        }


        /// <summary>
        /// Gets the constraints.
        /// </summary>
        /// <param name="route">The route.</param>
        /// <returns></returns>
        private static RouteValueDictionary GetConstraints(RouteConfigElement route)
        {
            return GetDictionaryFromAttributes(route.Constraints.Attributes);
        }


        /// <summary>
        /// Gets the defaults.
        /// </summary>
        /// <param name="route">The route.</param>
        /// <returns></returns>
        private static RouteValueDictionary GetDefaults(RouteConfigElement route)
        {
            return GetDictionaryFromAttributes(route.Defaults.Attributes);
        }


        /// <summary>
        /// Gets the data tokens.
        /// </summary>
        /// <param name="route">The route.</param>
        /// <returns></returns>
        private static RouteValueDictionary GetDataTokens(RouteConfigElement route)
        {
            return GetDictionaryFromAttributes(route.DataTokens.Attributes);
        }


        /// <summary>
        /// Gets the dictionary from attributes.
        /// </summary>
        /// <param name="attributes">The attributes.</param>
        /// <returns></returns>
        private static RouteValueDictionary GetDictionaryFromAttributes(Dictionary<string, string> attributes)
        {
            RouteValueDictionary dataTokensDictionary = new RouteValueDictionary();

            foreach (var dataTokens in attributes)
            {
                //ref : DefaultControllerFactory.GetControllerType
                if (dataTokens.Key == "Namespaces")
                {
                    dataTokensDictionary.Add(dataTokens.Key, dataTokens.Value.Split(','));
                }
                else
                {
                    dataTokensDictionary.Add(dataTokens.Key, dataTokens.Value);
                }
            }

            return dataTokensDictionary;

        }
    }
}

www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.