[英]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.