子文件夹中的控制器

Ima*_*ani 20 asp.net-mvc asp.net-mvc-3

我的区域在下面.仅突出显示相关部分.

在此输入图像描述

路线表

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        "SubFolder", // Route name
        "SubFolder/ChildController",
        new { controller = "ChildController", action = "Index" },
        new[] { "Practise.Areas.SOProblems.Controllers.SubFolder" });


    routes.MapRoute(
        "Default", // Route name
        "{controller}/{action}", // URL with parameters
        new { controller = "Home", action = "Index" } // Parameter defaults
    );
}
Run Code Online (Sandbox Code Playgroud)

这只适用于url是这样的

localhost:2474/SOProblems/ChildController/index 
Run Code Online (Sandbox Code Playgroud)

当url是这样时,这不起作用

localhost:2474/SOProblems/SubFolder/ChildController/index
Run Code Online (Sandbox Code Playgroud)

你能告诉我缺少什么吗?

Dar*_*rov 15

当url类似于localhost时,这不起作用:2474/SOProblems/SubFolder/ChildController/index

这很正常.路由模式看起来像这样:SubFolder/ChildController而不是SubFolder/ChildController/index.除此之外,您还在错误的位置定义了路线.您在主路径定义中定义了它,而不是在您的区域路径定义中定义它.因此,从主路径中删除自定义路由定义并将其添加到SOProblemsAreaRegistration.cs文件(SOProblems应该注册路由的位置):

public override void RegisterArea(AreaRegistrationContext context)
{
    context.MapRoute(
        "SubFolderRoute", 
        "SOProblems/SubFolder/ChildController",
        new { controller = "ChildController", action = "Index" },
        new[] { "Practise.Areas.SOProblems.Controllers.SubFolder" }
    );

    context.MapRoute(
        "SOProblems_default",
        "SOProblems/{controller}/{action}/{id}",
        new { action = "Index", id = UrlParameter.Optional }
    );
}
Run Code Online (Sandbox Code Playgroud)

此外,由于您的路由模式(SOProblems/SubFolder/ChildController)无法指定操作名称,因此您只能在此控制器上执行一个操作,这将是您index在此情况下注册的默认操作().

如果您想在此控制器上执行更多操作,但索引是默认操作,则应在路由模式中包含该操作:

context.MapRoute(
    "SubFolder", 
    "SOProblems/SubFolder/ChildController/{action}",
    new { controller = "ChildController", action = "Index" },
    new[] { "Practise.Areas.SOProblems.Controllers.SubFolder" }
);
Run Code Online (Sandbox Code Playgroud)

在这两种情况下,您的主路径定义都可以保留其默认值:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        "Default",
        "{controller}/{action}",
        new { controller = "Home", action = "Index" }
    );
}
Run Code Online (Sandbox Code Playgroud)

  • "RegisterArea"来自哪里?我搜索了整个解决方案,结果为零. (2认同)

Mat*_*ser 5

您的新路线"SubFolder"不包括在路线中包含操作的可能性(在您的情况下,"索引").

您的示例网址

localhost:2474/SOProblems/SubFolder/ChildController/index
Run Code Online (Sandbox Code Playgroud)

想要尝试匹配以下路线:

"SubFolder/ChildController/{action}"
Run Code Online (Sandbox Code Playgroud)

但是您的路线中不包含"{action}",因此它与您的路线不符.然后它尝试默认路由,这显然会失败.

尝试在您的路线中添加"{action}":

routes.MapRoute(
    "SubFolder", // Route name
    "SubFolder/ChildController/{action}",
    new { controller = "ChildController", action = "Index" },
    new[] { "Practise.Areas.SOProblems.Controllers.SubFolder" });
Run Code Online (Sandbox Code Playgroud)

或者从您的测试网址中删除"index".