在视图内部生成错误的动作链接

时间:2014-07-18 19:49:54

标签: asp.net-mvc asp.net-mvc-4 razor

enter image description here

我使用主_Layout.cshtml作为我的Index.cshtml,它位于“Areas - > Views - > Home”中。 由于此链接注销&登录生成错误(位于主_LogOnPartial.cshtml内)。

即。它生成为“http:// localhost / Admin / User / LogOff”但它应该生成为“http:// localhost / User / LogOff”

_LogOnPartial.cshtml代码

<div class="btn-group pull-right">
    @if (Request.IsAuthenticated)
    {
        <text>
        @* Welcome <strong>@User.Identity.Name</strong>!*@

        @Html.ActionLink("Log Off", "LogOff", "User", null, new { @class = "btn btn-action btn-first" })  </text>
    }
    else
    {
        <text>@Html.ActionLink("Log In", "Login", "User", null, new { @class = "btn btn-action btn-last" }) @Html.ActionLink("Register", "Register", "User", null, new { @class = "btn btn-action" })</text>
    }
</div>

1 个答案:

答案 0 :(得分:1)

如果要生成不以某个区域为目标的网址,则需要确保清除区域路由值。您可以通过将区域路由值设置为空字符串来实现此目的。

例如:

@Html.ActionLink("Log Off", "LogOff", "User", new { area = "" }, new { @class = "btn btn-action btn-first" })

这种方式无论您渲染的视图是否在某个区域内,都将始终为&#34; root&#34;生成共享部分中的链接。控制器。