开发者

Nested areas in MVC 2 / MVC 3 / MVC 4

开发者 https://www.devze.com 2023-01-07 00:41 出处:网络
Since MVC 2 we can create areas easily. Now my question is related to nested areas (areas inside of areas).

Since MVC 2 we can create areas easily. Now my question is related to nested areas (areas inside of areas).

Select my "father" area folder, Right-mouse-click > Add > NO option for a new Area.

Is it poss开发者_StackOverflow中文版ible to do it in some other way ? or will this option be available in the near future?


I realise this is an old question but I'll answer it in case anyone else is trying to figure it out. A solution to this is to create areas that use a different routing value at a lower level than area, so for example your RouteConfig would look something like this:

public class RouteConfig
    {
        /// <summary>
        /// A function that registers the default navigation route.
        /// </summary>
        /// <param name="routes">The RouteCollection to act on.</param>
    public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            var route = routes.MapRoute(
            name: "Default",
            url: "{area}/{subArea}/{controller}/{action}/{id}",
            defaults: new { area = "DefaultArea", controller = "Home", action = "Splash", id = UrlParameter.Optional, section = "Customer" },
            namespaces: new string[] { "Application.Controllers" });
        }
    }

And one of your sub-area registrations might look like this:

public class ApplicationSubAreaRegistration : AreaRegistration 
{
    public override string AreaName 
    {
        get 
        {
            return "ApplicationSubArea";
        }
    }

    public override void RegisterArea(AreaRegistrationContext context) 
    {
        context.MapRoute(
            "SubArea_default",
            "Area/SubArea/{controller}/{action}/{id}",
            new { action = "Index", id = UrlParameter.Optional },
            new string[] { "Application.Areas.AreaName.SubAreaName.Controllers" }
        );
    }
}

After reading that, does "area" still look like a word? Because it doesn't to me.

P.S. You can do this recursively as many times as you like (theoretically) such that for example you could do

url: "{area}/{subArea}/{subSubArea}/{subSubSubArea}/{evenMoreSubArea}/{controller}/{action}/{id}",

in your RouteConfig.cs and

"Area/SubArea/SubSubArea/SubSubSubArea/EvenMoreSubArea/{controller}/{action}/{id}",

in your area registration.


For now there isn't any information telling if there will be nested areas.

In the future maybe this will change.


Using the idea of Multi-project areas as a start, I guess you could recursively create more nested areas.


Maybe something like this could help. It's more like a study which is in mvc-contrib. I saw it for version 1 don't know if it's compatible for MVC2 It's the concept of sub-controllers: http://mhinze.com/subcontrollers-in-aspnet-mvc/


At this time MVC supports only Main Application and then Areas in next level and NOT nested Areas, but you can look at This Nuget Package that adds the following functionality to your project:

  • Organize your controllers and views using namespaces (no more areas) that can go as deep as you want.
  • Default constraints for primivite types that can be overridden on a per-parameter or per-site basis.
  • Intelligent grouping of similar routes for efficient matching.
  • Support for a root controller.
  • Support for overloaded actions.
  • Support for hierarchical (a.k.a. RESTful) routes.
  • Support for user-defined custom routes.
  • Detection of ambiguous routes.
  • Formatting of routes (e.g. to lowercase, hyphen-separated, underscore-separated, etc).
  • Render your routes as calls to the MapRoute extension method, for debugging.
  • Support for embedded views (as assembly resources).


You do not want to have nested aereas. There is something wrong in your Software design.

the most common case is, that you use areas as Html Renderer, therefore are the Display Templates.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号