如何防止在mvc url中显示“索引”

时间:2014-12-20 11:41:25

标签: asp.net-mvc asp.net-mvc-4 asp.net-mvc-routing

我在MVC4网站上使用ActionLink,如下所示:

@Html.ActionLink("home", result: MVC.Home.Index())

重定向后,网址以“www.mysite.com/home/index”形式显示。

如何阻止在url中显示“home / index”或“index”

2 个答案:

答案 0 :(得分:1)

您在url中看到了索引,因为html.actionlink使用href home / index生成标记。而不是使用帮助程序html.actionlink直接使用标记与href作为home并具有路由到索引的默认路由。但在这种情况下,您可能无法在虚拟目录中部署时正确路由。

答案 1 :(得分:0)

您必须在路线配置中为相应的路线提供默认值,如下所示:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    routes.MapRoute(name: "Default",  url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );
}
相关问题