Get Areas associated with an MVC project

前端 未结 3 1139
旧巷少年郎
旧巷少年郎 2021-01-12 17:13

Is there a way I can get the names of the areas in an MVC project?

Here\'s a few ways I can think of:

a) If I had the source, I could browse through the proj

相关标签:
3条回答
  • 2021-01-12 17:30

    It would seem that the only way you will be able to retrieve the routes registered in your project is by enumerating the project for types which inherit AreaRegistration, there does not appear to be any object private or public which tracks currently registered areas.

    Long explanation follows...

    One hurdle to keep in mind here is that areas are little more than a coupling between an arbitrary string and a list of namespaces. When an area is registered it is merely extending the route collection for the application with some new rules identifiable by a unique "area" DataToken.

    If you look at the process for registering an area, you must inherit from System.Web.Mvc.AreaRegistration and override RegisterArea(). RegisterArea() receives an AreaRegistrationContext which defines an area name, route collection and object state, but if you observe the format for implementing RegisterArea(), it returns void and does nothing to preserve the context object. What's more, if you look at the code which runs before RegisterArea() is fired (Reflector), you can see that the AreaRegistrationContext object which is passed to RegisterArea() is never permanently tracked.

    internal static void RegisterAllAreas(RouteCollection routes, IBuildManager buildManager, object state)
    {
        foreach (Type type in TypeCacheUtil.GetFilteredTypesFromAssemblies("MVC-AreaRegistrationTypeCache.xml", new Predicate<Type>(AreaRegistration.IsAreaRegistrationType), buildManager))
        {
            ((AreaRegistration) Activator.CreateInstance(type)).CreateContextAndRegister(routes, state);
        }
    }
    
    internal void CreateContextAndRegister(RouteCollection routes, object state)
    {
        AreaRegistrationContext context = new AreaRegistrationContext(this.AreaName, routes, state);
        string str = base.GetType().Namespace;
        if (str != null)
        {
            context.Namespaces.Add(str + ".*");
        }
        this.RegisterArea(context);
    }
    

    As you can see, a call to the static method RegisterAllAreas() invokes the internal RegisterAllAreas(RouteCollection routes, IBuildManager buildManager, object state), which then calls the internal CreateContextAndRegister(RouteCollection routes, object state), which creates the AreaRegistrationContext and passes it to RegisterArea().

    As far as I can tell, never, at any point, is the AreaRegistrationContext created for each area stored permanently.

    0 讨论(0)
  • 2021-01-12 17:34

    This thread was helpful in finding the answer, but nobody here posted it explicitly. Here is what I came up with, but do note you may need to adjust it depending on whether all of your areas are in the same assembly.

    private IEnumerable<AreaRegistration> GetAllAreasRegistered()
    {
        var assembly = this.GetType().Assembly;
        var areaTypes = assembly.GetTypes().Where(t => t.IsSubclassOf(typeof(AreaRegistration)));
        var areas = new List<AreaRegistration>();
        foreach (var type in areaTypes)
        {
            areas.Add((AreaRegistration)Activator.CreateInstance(type));
        }
        return areas;
    }
    
    0 讨论(0)
  • 2021-01-12 17:43

    You could do what MVC is doing, iterate all the referenced assemblies and look for classes that inherit from AreaRegistration. Then you can simply get the AreaRegistration.AreaName.

    I'm planning on doing this to build my top-level navbar using Twitter Bootstrap.

    0 讨论(0)
提交回复
热议问题