【问题标题】:MVC5 Route To Null Area Not Working到空区域的 MVC5 路由不起作用
【发布时间】:2014-05-28 20:48:38
【问题描述】:

我正在使用带有 Web Api“扩展”的默认 MVC5 Web 应用程序。该解决方案带有一个“web”帐户控制器、“web”主页控制器、一个“api”值控制器以及一个带有自己控制器的帮助区域。帮助控制器在 html 中详细说明了我的 api。

这都是默认的。

我的问题是当我在帮助区域时,我的一些操作链接(在顶部菜单上)不起作用。我希望顶部菜单(登录等)上的操作返回到默认的非区域路线,但他们没有。他们改为重定向到帮助区域。导航到根目录的“应用程序名称”有效,但 Contact、About、Login、Register (ext) 无效。

我已尝试将操作链接更改为指向“空”区域,但这无济于事。

@Html.ActionLink("Home", "Index", "Home", new { area = "" }, null)

当我在帮助区域中时,操作链接没有正确的 URI。当我将鼠标悬停在主页操作链接上时,它会将我重定向到 localhost:port/Help。我希望操作链接重定向回默认控制器路由(无区域,主页/帐户控制器)。

部分登录

@using Microsoft.AspNet.Identity
@if (Request.IsAuthenticated)
{
    using (Html.BeginForm("LogOff", "Account", FormMethod.Post, new { id = "logoutForm", @class = "navbar-right" }))
    {
    @Html.AntiForgeryToken()

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

【问题讨论】:

  • 您能否详细说明您所说的不工作是什么意思?即 404、错误的视图渲染等...
  • 当然。当我在帮助区域中时,操作链接没有正确的 URI。当我将鼠标悬停在主页操作链接上时,它会将我重定向到 localhost:port/Help。我希望操作链接重定向回默认控制器路由(无区域,主页/帐户控制器)。
  • 您是否正在从根 /views/ 文件夹中的共享部分渲染菜单?
  • @BrentMannering 是的,我是。 LoginPartial 和 Layout 都位于共享文件夹中。
  • 您能否发布 LoginPartial 的代码(假设这是问题链接所在的位置)?

标签: asp.net-mvc asp.net-mvc-areas


【解决方案1】:

我唯一能确定LoginPartial 中可能的罪魁祸首是area 在htmlAttributes 中设置的属性,而不是routeValues。不过我猜这可能是因为尝试了不同的东西。

修改routeValues 似乎对我有用:

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

    <ul class="nav navbar-nav navbar-right">
        <li>
            @Html.ActionLink("Hello " + User.Identity.GetName() + "!", "Manage", "Account", routeValues: new { area = "" }, htmlAttributes: new { title = "Manage" })
        </li>
        <li><a href="javascript:document.getElementById('logoutForm').submit()">Log off</a></li>
    </ul>
    }
}
else
{
    <ul class="nav navbar-nav navbar-right">
        <li>@Html.ActionLink("Register", "Register", "Account", routeValues: new { area = "" }, htmlAttributes: new { id = "registerLink" })</li>
        <li>@Html.ActionLink("Log in", "Login", "Account", routeValues: new { area = "" }, htmlAttributes: new { id = "loginLink" })</li>
    </ul>
} 

【讨论】:

  • 再次感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 2017-09-29
  • 2013-12-29
  • 2014-12-27
  • 2016-01-06
  • 2019-03-05
  • 1970-01-01
  • 1970-01-01
  • 2013-12-26
相关资源
最近更新 更多