Asp.net mvc 如何防止“显示”;索引“;在mvc url中

Asp.net mvc 如何防止“显示”;索引“;在mvc url中,asp.net-mvc,asp.net-mvc-4,asp.net-mvc-routing,Asp.net Mvc,Asp.net Mvc 4,Asp.net Mvc Routing,我在MVC4网站中使用ActionLink,如下所示: @Html.ActionLink("home", result: MVC.Home.Index()) 重定向后,url以“www.mysite.com/home/index”的形式出现 如何防止在url中显示“主页/索引”或“索引”?您必须在路由配置中为相应的路由提供默认值,如下所示: public static void RegisterRoutes(RouteCollection routes) { routes.Ignore

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

@Html.ActionLink("home", result: MVC.Home.Index())
重定向后,url以“www.mysite.com/home/index”的形式出现


如何防止在url中显示“主页/索引”或“索引”?

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

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 }
    );
}

您正在url中看到索引,因为html.actionlink使用href home/index生成标记。不要使用helper html.actionlink,而是直接使用标签和href作为主页,并有一个路由到索引的默认路由。但在这种情况下,在虚拟目录中部署时,可能会失去正确路由的能力