public class HomeController : Controller
{
[Route("Users/about")]
[Route("Users/WhoareWe")]
[Route("Users/OurTeam")]
[Route("Users/aboutCompany")]
public ActionResult GotoAbout()
{
return View();
}
}
我为行动GotoAbout()
定义了许多路线。
如何为home/users/about
等行为生成网址时,如何以编程方式在剃刀页面中创建路径网址?
答案 0 :(得分:5)
参考Attribute Routing in ASP.NET MVC 5 - Route Names
您可以指定路由的名称,以便轻松地为其生成URI。
例如,对于以下路线:
[RoutePrefix("Home")]
public class HomeController : Controller {
[Route("Users/about", Name = "Users_About")]
[Route("Users/WhoareWe")]
[Route("Users/OurTeam")]
[Route("Users/aboutCompany")]
public ActionResult GotoAbout() {
return View();
}
}
您可以使用Url.RouteUrl
生成链接:
<a href="@Url.RouteUrl("Users_About")">About</a>
将解析为
<a href="home/users/about">About</a>