Si *_*xxx 3 model-view-controller routing attributes root
我已将Visual Studio 2013模板"Home"控制器移动到其自己的区域中.
我正在尝试制作一个指向HomeController上的动作的ActionLink.
但是,而不是链接呈现为:
www.site.com/Home/ActionName
我希望它呈现为
www.site.com/ActionName
(对于控制器中的所有操作).
这样,我的网站的根目录中不包含"主页".
我正在尝试使用属性路由部署我的路由,但是我对如何执行此操作感到迷茫,正确的方向上的任何一点都将受到赞赏.
好的,所以我想出来了:
在我的控制器上用作站点根目录(例如"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();
}
}
Run Code Online (Sandbox Code Playgroud)
ActionLink示例(来自上面的代码):
现在将呈现为www.site.com
@Html.ActionLink("Home", "Index", "Home")
Run Code Online (Sandbox Code Playgroud)
这将呈现为www.site.com/About
@Html.ActionLink("About", "About", "Home")
Run Code Online (Sandbox Code Playgroud)
这将呈现为www.site.com/Home/Home?action=Contact
@Html.ActionLink("Contact", "Contact", "Home")
Run Code Online (Sandbox Code Playgroud)
我希望这有助于其他人.谢谢.
| 归档时间: |
|
| 查看次数: |
1321 次 |
| 最近记录: |