MVC 2/MVC 3/MVC 4中的嵌套区域 [英] Nested areas in MVC 2 / MVC 3 / MVC 4

查看:58
本文介绍了MVC 2/MVC 3/MVC 4中的嵌套区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自MVC 2起,我们可以轻松创建区域.现在我的问题与嵌套区域(区域内部的区域)有关.

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

选择我的"father"区域文件夹,右键单击Add> new Area的NO选项.

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

是否可以通过其他方式实现?还是在不久的将来会提供此选项?

Is it possible to do it in some other way ? or will this option be available in the near future?

推荐答案

我意识到这是一个古老的问题,但如果有人尝试找出答案,我会回答.一种解决方案是创建使用比区域更低级别的路由值的区域,例如,您的RouteConfig看起来像这样:

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.您可以(理论上)根据需要多次进行递归操作,例如可以

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}",

在RouteConfig.cs和

in your RouteConfig.cs and

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

在您所在地区的注册.

这篇关于MVC 2/MVC 3/MVC 4中的嵌套区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆