我有以下类型的mvc应用程序Areas文件夹结构:
区域=>文件夹A =>控制器=>控制器A和控制器B
我在AreaRegistration中使用了以下注册路径:
context.MapRoute(
"default1",
"FolderA/{controller}/{action}/{id}",
new { controller = "ControllerA|ControllerB", action = "Index", id = UrlParameter.Optional }
);
我在共享布局上有两个链接,分别是:
@Html.ActionLink("Link 1", "ActionA", "ControllerA", null)
@Html.ActionLink("Link 2", "ActionB", "ControllerB", null)
链接1似乎工作正常,并按预期进行了重定向。问题是Link2,它始终形成以下网址,并且出现404错误。
http:// localhost:29661 / FolderA / ControllerA / ActionB?Length = 15
默认的应用程序路由路径为:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
似乎它总是在同一控制器中寻找ActionB,即使我注册了2条不同的路径也是如此。任何人都可以帮忙。
ControllerA|ControllerB
不是有效的默认值。我相信您想要的是一个约束,而不是您的控制器的默认设置。因此,您的路线应改为:
context.MapRoute(
"default1",
"FolderA/{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new { controller = "ControllerA|ControllerB" }
);
但是在这种情况下,您不需要约束,因为要使用的路由可以由区域名称确定,因此可以通过以下方式获得:
context.MapRoute(
"default1",
"FolderA/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
如果URL中未提供控制器,则用于控制器的默认位置将是其导航到的位置。您可以使用您所在区域的控制器作为默认控制器,也可以排除默认控制器以要求URL中包含控制器。
在任何情况下,构建到区域的链接时都需要提供区域名称。当您提供字符串作为第三个参数时,您正在使用的重载将不起作用。
@Html.ActionLink("Link 1", "ActionA", "ControllerA", new { area = "FolderA" }, null)
@Html.ActionLink("Link 2", "ActionB", "ControllerB", new { area = "FolderA" }, null)
就像这里指出的,即使您要导航到非区域链接,也需要指定区域。
@Html.ActionLink("Home", "Index", "Home", new { area = "" }, null)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句