我有一个区域注册路由:
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"MyArea_default",
"MyArea/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new string[] { "market.Areas.MyArea.Controllers" }
);
}
它的工作原理,它的确定:
localhost:MyArea/ActionName
但是当我选择一个特定的视图时,我点击了F5(在visual studio中),然后url是:
localhost:MyArea/Home/ActionName
那么如果选择特定的视图,当我点击f5时,如何在没有控制器名称的情况下工作?
2 回答
我从你的代码中理解的是:
localhost:MyArea / ActionName =>将使用带有ActionName方法的homeControllerClass .
localhost:MyArea / Home / ActionName =>无法工作,因为它使用homeControllerClass,但ActionName为“Home”!!那就是在homeControllerClass中寻找home方法 .
从您的代码:“MyArea / / ”new {controller =“Home”,action =“Index”,id = UrlParameter.Optional}
=>如果没有指定任何操作,它意味着使用homecontroller类来获取MyArea的请求,其默认操作名称为index . 因此,如果您使用MyArea / Home =>,它将使用homeController但使用“Home”操作 .
{控制器} / {操作}
有很多解决方案 . 最简单的是你可以使用服装路线属性