gpt4 book ai didi

asp.net-mvc - 子文件夹中的 Controller

转载 作者:行者123 更新时间:2023-12-03 12:15:26 26 4
gpt4 key购买 nike

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

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

你能告诉我缺少什么吗?

最佳答案

This does not works when the url is like this localhost:2474/SOProblems/SubFolder/ChildController/index



这是正常的。您的路由模式如下所示: SubFolder/ChildController而不是 SubFolder/ChildController/index .除此之外,您在错误的地方定义了路线。您在主路线定义中而不是在区域路线定义中定义了它。因此,从您的主要 route 删除自定义路线定义并将其添加到 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 上有更多的 Action ,但 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com