我已将 Visual Studio 2013 模板“Home” Controller 移动到它自己的区域中。
我正在尝试创建一个指向 HomeController 上的操作的 ActionLink。
但是,链接不是呈现为:
www.site.com/Home/ActionName
我希望它呈现为
www.site.com/ActionName
(对于 Controller 中的所有操作)。
这样我网站的根目录就不会在链接中包含“主页”。
我正在尝试使用属性路由来部署我的路由,但是我不知道我是如何做到这一点的,任何指向正确方向的观点都会受到赞赏。
最佳答案
好的,所以我想通了:
在我的 Controller 上用作站点根目录(例如“HomeController”):
[RouteArea("Home")] // Area
[Route("Home")] // Controller
public class HomeController : Controller
{
[Route("~/")] // Rendered path of the Index Action www.site.com/
public ActionResult Index()
{
return View();
}
[Route("~/About")] // Rendered path of the About Action www.site.com/About
public ActionResult About()
{
ViewBag.Message = "Your application description page.";
return View();
}
// ...And with no routing attribute on the Contact Action below,
// The rendered path will remain in the format:
// www.site.com/Home/Home?action=Contact
public ActionResult Contact()
{
ViewBag.Message = "Your contact page.";
return View();
}
}
ActionLink 示例(来自上面的代码):
这现在将呈现为 www.site.com
@Html.ActionLink("Home", "Index", "Home")
这将呈现为 www.site.com/About
@Html.ActionLink("About", "About", "Home")
这将呈现为 www.site.com/Home/Home?action=Contact
@Html.ActionLink("Contact", "Contact", "Home")
我希望这对其他人有帮助。谢谢。
关于model-view-controller - 如何使用属性路由使 ASP.NET MVC 5 区域显示为站点根目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23816279/