[英]Call ActionResult in the Area controller from a shared view
从Views文件夹(区域外部)内的Layout.cshtml中,我需要解决在区域内部创建的控制器的操作方法。
我定义了一些自定义路由,但是默认路由仍然一如既往:
routes.MapRoute("Default", "{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional });
例如:
区 - 测试 , 控制器 - HomeController中 , 操作方法 - 指数
@Url.Action("Index", "Home", new { Area = "Test" })
返回一个空字符串。 @Html.ActionLink("Test link", "Index", "Home")
返回一个空字符串。
@Html.RouteLink("Test Link", "default", new { controller = "Home", action = "Index" })
返回错误
在路由集合中找不到名为“默认”的路由。
只是为了澄清,此操作不需要使用自定义路由来调用。 永远不会通过URL直接访问它,因此默认路由应该可以。
我很伤心。 如何解决该问题?
在同一名称空间下添加以下类
public class TestAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "Test";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Test_default",
"Test/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
}
在Global.asax下,确保具有RegisterAllAreas()
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.