Asp.net

ASP.net MVC - 在區域之間共享部分

  • March 5, 2011

有什麼方法可以在區域之間共享部分剃刀視圖?

例如登錄部分,如果我使用@Html.Partial("_LoginPartial")但生成的 URLHtml.ActionLink是呼叫區域的本地(即使部分本身不是該區域的一部分),則會找到它。

_LoginPartial.cshtml is in /Views/Shared/_LoginPartial.cshtml
Calling view is inside /Areas/Somearea/Views

Links generated are like: http://example.com/Somearea/Account/Login
But should always be: http://example.com/Account/Login

部分視圖源:

@if(Request.IsAuthenticated) {
   <text>Welcome <b>@Context.User.Identity.Name</b>!
   [ @Html.ActionLink(@Messages.Logout, "Logout", "Account") ]</text>
}
else {
   @:[ @Html.ActionLink(@Messages.Login, "Login", "Account") ]
}

謝謝

您可以在方法中指定區域(或缺少區域)ActionLink()

Html.ActionLink(@Messages.Logout, "Logout", "Account", new { Area = "" }, new{})

這將確保連結不會解析為目前區域內的 URL。

引用自:https://stackoverflow.com/questions/5200622