从 MVC 5 中的 URL 中删除控制器 'Home'

Remove controller 'Home' from URL in MVC 5

我需要从我项目的 URL 中删除控制器 'Home',现在它看起来像这样:example。com/Home/contact 我希望它像这样:example .com/contact(也适用于服务、我们的团队、画廊等其他部分)

我试图删除 RouteConfig.cs 代码中出现在“{action}/{id}”之前的“{controller}”,如下所示:

namespace myproject
{
public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            "Default",
            "{action}/{id}",
            new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }
    }
}

并且控制器 'Home' 从 URL 中消失,但以下登录和管理链接不起作用:

@Html.ActionLink("Login here", "Login", "Account")
@Html.ActionLink("Manage website", "Index", "Manage")

如何在不在 URL 中显示控制器 'Home' 的情况下使这些链接正常工作?

您需要将以下内容添加到您的 RouteConfig.cs

    routes.MapRoute(
        name: "contact",
        url: "contact",
        defaults: new { controller = "Home", action = "Contact" }
    );

确保 "Default" 路由映射是最后一个。