从区域内视图访问根控制器操作不适用于 area = ""

Access root controller action from view inside area does not work with area = ""

我在名为 "Application" 的区域中查看。

在这个视图中我有这个 Link:

@using (Html.BeginForm("LogOff","Account", FormMethod.Post, new { area = "",  id = "logoutForm", @class = "navbar-right" }))
{
    @Html.AntiForgeryToken()

    <ul class="nav navbar-nav navbar-right">
        <li>
            @Html.ActionLink("Hello " + User.Identity.GetUserName() + "!", "Index", "Manage", routeValues: null, htmlAttributes: new { title = "Manage" })
        </li>
        <li><a href="javascript:document.getElementById('logoutForm').submit()">Log off</a></li>
    </ul>
}

此操作Link 引用具有操作 "LogOff" 的根控制器 "AccountController"。

当我提交表单时,浏览器会在 URL 栏中显示:

http://localhost:61774/Application/Account/LogOff

但是这条路线不存在,因为尽管我设置了 area = ""

,但它 NOT 会查看 Root 控制器

为什么不起作用?

您正在使用 this overload of Html.BeginForm() which is adding area as a html attribute, not a route value. You need to use this overload 添加路由值

@using (Html.BeginForm("LogOff","Account", new { area = "" }, FormMethod.Post, new { id = "logoutForm", @class = "navbar-right" }))