C# ASP.NET MVC 5路由在使用RouteArea后停止工作
我使用Visual Studio 2015创建了一个新的ASP.NET MVC 5。项目中的所有代码都是默认代码,Microsoft将其作为其模板的一部分提供,但当我添加一个名为UserController的新控制器,并带有[RouteArea(“Admin”)]时,其他链接将停止工作,例如Home/Contact、Home/About,只要我访问管理区域(/Admin/User/Index)但是如果我手动输入地址栏,那么About和Contact链接可以正常工作。仅当您输入的控制器已成为RouteArea属性时。我是MVC新手,但我对WebForms更为熟悉,因为我多年来一直在使用它构建网站 我甚至在操作上方添加了[Route]和[HttpGet],以查看这是否解决了问题,但没有起作用 shared/_layout.cshtml中的链接仍然是默认链接,在导航到具有[RouteArea]的控制器之前解析URL:C# ASP.NET MVC 5路由在使用RouteArea后停止工作,c#,asp.net,asp.net-mvc,asp.net-mvc-4,C#,Asp.net,Asp.net Mvc,Asp.net Mvc 4,我使用Visual Studio 2015创建了一个新的ASP.NET MVC 5。项目中的所有代码都是默认代码,Microsoft将其作为其模板的一部分提供,但当我添加一个名为UserController的新控制器,并带有[RouteArea(“Admin”)]时,其他链接将停止工作,例如Home/Contact、Home/About,只要我访问管理区域(/Admin/User/Index)但是如果我手动输入地址栏,那么About和Contact链接可以正常工作。仅当您输入的控制器已成为Rou
@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>
一旦我进入下面的控制器,上面提到的Html.ActionLink()就无法解析上面控制器的URL
UserController.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();
}
}
[RouteArea("Admin")]
[RoutePrefix("User")]
public class UserController : Controller
{
//GET Admin/User/Index
[HttpGet]
[Route("Index")]
public ActionResult Index()
{
return View();
}
}
进入UserController后,顶部导航菜单的Html.ActionLinks的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>
@Html.ActionLink(“主页”、“索引”、“主页”)/>
@Html.ActionLink(“关于”、“关于”、“主页”)/=>
@Html.ActionLink(“联系人”、“联系人”、“主页”)/>
从右侧上面3行中的html输出可以看出,没有URL
任何帮助都将不胜感激。谢谢。在使用区域时,我建议使用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 {})
或
这样,当您导航到某个区域时,它们仍然可以工作。答案是(感谢@AntDC和@flicksim为我指明了正确的方向):
@Html.ActionLink(“主页”、“索引”、“主页”,新{area=”“},空)
@ActionLink(“关于”,“关于”,“主页”,新{area=”“},空)
@ActionLink(“联系人”、“联系人”、“主页”,新{area=”“},空)
无论出于何种原因,ASP.NET都将新的{area=”“}作为html属性,即使有一个带有RouteValue精确重载的签名。是否需要指定“area”?例如@Html.ActionLink(“User”、“Index”、“User”、new{area=“Admin”})Html.ActionLink会在我到达后(Admin/User/Index)无任何问题地解析用户管理区域;回家,联系,关于停止工作。我会试试看这是否有用。谢谢你的回复。我试过了,但没用。HTML输出如下:Home=>About=>Contact=>mybad,我使用了错误的重载。我会更新我的答案
@Html.ActionLink("Home, "Index", new { controller="Home", area=""})
@Html.ActionLink("About, "About", new { controller="Home", area=""})
@Html.ActionLink("Contact, "Contact", new { controller="Home", area=""})
<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>