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

路由表

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

仅当网址是这样时才有效
localhost:2474/SOProblems/ChildController/index

当网址是这样时,这不起作用
localhost:2474/SOProblems/SubFolder/ChildController/index

你能告诉我缺少什么吗?

最佳答案



那很正常您的路由模式如下所示:SubFolder/ChildController而不是SubFolder/ChildController/index。除此之外,您还在WRONG位置定义了路线。您是在主路径定义中定义的,而不是在区域路径定义中定义的。因此,从主路由中删除自定义路由定义,然后将其添加到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)无法指定操作名称,因此您只能在此 Controller 上执行一个操作,在这种情况下,这将是您注册的默认操作(index)。

如果您想对该 Controller 执行更多操作,但仍将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" }
    );
}

关于asp.net-mvc - 子文件夹中的 Controller ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17178688/

10-10 18:57