【问题标题】:ASP.NET MVC 5 Routing stops working after using RouteAreaASP.NET MVC 5 路由在使用 RouteArea 后停止工作
【发布时间】:2017-07-31 18:22:26
【问题描述】:

我使用 Visual Studio 2015 创建了一个新的 ASP.NET MVC 5。项目中的所有代码都是默认的,Microsoft 作为其模板的一部分提供,但是当我使用 [RouteArea("Admin ")] 一旦我访问管理区域(/Admin/User/Index),其他链接将停止工作,例如 Home/Contact、Home/About,但如果我在地址栏中手动输入 http://localhost/,那么 About 和 Contact 链接工作正常。仅当您输入一个具有 RouteArea 属性的控制器时。我是 MVC 的新手,但我对 WebForms 更熟悉,因为多年来我一直使用它来构建网站。

我什至在动作上方添加了 [Route] 以及 [HttpGet] 以查看是否可以解决问题,但没有奏效。

shared/_layout.cshtml 中的链接仍然是默认的,它在导航到具有 [RouteArea] 的控制器之前解析 URL:

@Html.ActionLink("Home", "Index", "Home") // => <a href="/">Home</a>
@Html.ActionLink("About", "About", "Home") // => <a href="/About">About</a>
@Html.ActionLink("Contact", "Contact", "Home") // => <a href="/Contact">Contact</a>

HomeController.cs:

public class HomeController : Controller
{
    //GET /
    [HttpGet]
    [Route]
    public ActionResult Index()
    {
        return View();
    }

    //GET /About
    [HttpGet]
    [Route("About")]
    public ActionResult About()
    {
        ViewBag.Message = "Your application description page.";

        return View();
    }

    //GET /Contact
    [HttpGet]
    [Route("Contact")]
    public ActionResult Contact()
    {
        ViewBag.Message = "Your contact page.";

        return View();
    }
}

一旦我进入下面的控制器,上面提到的Html.ActionLink()就无法解析到上面控制器的URL。

用户控制器.cs:

[RouteArea("Admin")]
[RoutePrefix("User")]
public class UserController : Controller
{
    //GET Admin/User/Index
    [HttpGet]
    [Route("Index")]
    public ActionResult Index()
    {
        return View();
    }
}

顶部导航菜单的Html.ActionLinks,进入UserController后,href属性都为空,如下图:

@Html.ActionLink("Home", "Index", "Home") // => <a href="">Home</a>
@Html.ActionLink("About", "About", "Home") // => <a href="">About</a>
@Html.ActionLink("Contact", "Contact", "Home") // => <a href="">Contact</a>

从上面3行右侧的html输出中可以看出,没有URL。

任何帮助将不胜感激。谢谢。

【问题讨论】:

  • 是否需要指定“区域”?例如。 @Html.ActionLink("User", "Index", "User", new {area = "Admin"})
  • Html.ActionLink 解决了用户管理区域没有任何问题,但是一旦我在那里(管理员/用户/索引);回家,联系,关于停止工作。我会尝试看看是否有帮助。谢谢

标签: c# asp.net asp.net-mvc asp.net-mvc-4


【解决方案1】:

在使用区域时,我建议使用允许您指定区域的 ActionLink 的重载。

因此,指向根目录的导航链接将是:

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

或者

@Html.ActionLink("Home, "Index", new { controller="Home", area=""})
@Html.ActionLink("About, "About", new { controller="Home", area=""})
@Html.ActionLink("Contact, "Contact", new { controller="Home", area=""})

这样,当您导航到某个区域时,它们仍然可以工作。

【讨论】:

  • 感谢您的回复。我试过了,但没有用。 HTML 输出如下: Home => Home About => About 联系方式 => 联系方式
  • 我的错,我使用了错误的重载。我会更新我的答案
【解决方案2】:

答案是(感谢@AntDC 和@Slicksim 为我指明了正确的方向):

<li>@Html.ActionLink("Home", "Index", "Home", new {area = ""}, null)</li>
<li>@Html.ActionLink("About", "About", "Home", new { area = "" }, null)</li>
<li>@Html.ActionLink("Contact", "Contact", "Home", new { area = "" }, null)</li>

无论出于何种原因,ASP.NET 都将新的 {area = ""} 作为 html 属性,即使存在具有这种精确重载 routeValues 的签名。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-17
    • 1970-01-01
    • 2017-11-08
    • 1970-01-01
    • 2020-12-11
    相关资源
    最近更新 更多