Tips: Area registration order (here solution) RRS feed

  • Question

  • User-1664373945 posted

    MVC3 RegisterAllAreas() scan assemly for AreaRegistration and register routes.

    No way to specify area routes order. But this order critical important to select right controler.

    In most cases you have small set of areas and no hard work to manualy register its.

    Here is small helpfull code to easy register areas with right ordering.

    public static class Utils
        public static void RegisterArea<T>(RouteCollection routes, object state) where T : AreaRegistration
               AreaRegistration registration = (AreaRegistration)Activator.CreateInstance(typeof(T));
               AreaRegistrationContext context = new AreaRegistrationContext(registration.AreaName, routes, state);
               string tNamespace = registration.GetType().Namespace;
               if (tNamespace != null)
                    context.Namespaces.Add(tNamespace + ".*");

    Now you can replace in Application_Start:

    Utils.RegisterArea<SystemAreaRegistration>(RouteTable.Routes, null);
    Utils.RegisterArea<ClientSitesAreaRegistration>(RouteTable.Routes, null);
    // don't register automaticaly now -- AreaRegistration.RegisterAllAreas();

    Saturday, January 15, 2011 6:23 PM

All replies

  • User197322208 posted

    please format your code.

    Sunday, January 16, 2011 4:46 AM