子文件夹中的控制器

25

下面是我的区域。只有相关部分被突出显示。

enter image description here

路由表

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
    );
}

只有在URL是这样的情况下才能起作用。
localhost:2474/SOProblems/ChildController/index 

当URL地址为此时,此方法不起作用。
localhost:2474/SOProblems/SubFolder/ChildController/index

请问您缺少什么?


“Does not work” 意味着什么? - TGlatzer
@Grumbler85 - 最后尝试的URL返回404错误。 - Imad Alazani
反引号只在 "SubFolder`/ChildController" 中使用?请检查子文件夹中的命名空间是否设置正确。 - TGlatzer
是的,没错。有个打字错误。现在已经更正了。谢谢。 - Imad Alazani
3个回答

18

当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 }
    );
}

此外,由于您的路由模式(SOProblems/SubFolder/ChildController)没有指定动作名称的可能性,因此您只能在该控制器上拥有一个动作,即您在此情况下注册的默认动作(index)。

如果您想在此控制器上拥有更多的动作,并且仍然希望索引成为默认动作,则应将其包含在路由模式中:

context.MapRoute(
    "SubFolder", 
    "SOProblems/SubFolder/ChildController/{action}",
    new { controller = "ChildController", action = "Index" },
    new[] { "Practise.Areas.SOProblems.Controllers.SubFolder" }
);
在这两种情况下,您的主路由定义可以保持默认值:
public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        "Default",
        "{controller}/{action}",
        new { controller = "Home", action = "Index" }
    );
}

2
“RegisterArea”这个东西是从哪里来的?我在整个解决方案中搜索了一遍,但没有任何结果。 - usefulBee
@usefulBee,“RegisterArea”是在Visual Studio中新建_Add > Area..._时生成的。 - Jasen

6

您的新路由“SubFolder”不包含在路由中包含操作的可能性(在您的情况下,“Index”)。

您的示例网址

localhost:2474/SOProblems/SubFolder/ChildController/index

希望尝试匹配类似以下的路由:

"SubFolder/ChildController/{action}"

但是你的路由中没有包含"{action}",所以它不会匹配你的路由。然后它尝试默认路由,显然失败了。

尝试在你的路由中添加"{action}":

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

或者从您的测试URL中去掉“index”这个词。

3

1
这正是我所寻找的。谢谢。 - user4593252

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接