我认为最好的方法是:
routes.MapRoute("home", "home", new { controller = "Home", action = "Index" });
routes.MapRoute("about", "about", new { controller = "Home", action = "About" });
routes.MapRoute("contact", "contact", new { controller = "Home", action = "Contact" });
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
当您要创建链接时,请使用:
@Html.RouteLink("Home", "home", new{/* route values */}, new {/* html attribues */})
或者:
@Html.RouteLink("Home", "home")
代替:
@Html.ActionLink("Home", "Index", "Home", new{/* route values */}, new {/* html attribues */})
这对我有用,也应该对你有用。
更新:
您可以在 url 中的 action 部分之前创建一个类似 @(或 - 或其他任何东西)的符号,以使 url 唯一,例如:
routes.MapRoute(
"test", // route name
"@{action}", // url and parameters
new {controller = "MyHome", action = "Home"} // parameter defaults
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
通过这种方式,您的网址与默认地图路线不同,您可以创建如下网址:
site.com/@Home
site.com/@About
site.com/@Contact
但在我看来,第一个更好,我总是使用它。