如何在MVC5中设置默认路由

How to set up default route in MVC5

我无法理解 MVC5 中的默认路由设置。我在 RouteConfig.cs 文件

中有这段代码
public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        name: "Content",
        url: "content/{action}",
        defaults: new { controller = "Content", action = "Index" }
    );

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

当我转到 www.mydomain.com/content/index 时,它工作正常。但是,如果我转到 www.mydomain/content,我会收到错误 403.14 Forbidden。我知道这是一个新手问题,但我错过了什么?它不应该默认为索引控制器吗?

如果您使用的是默认 Asp.Net MVC5 项目模板,那么项目中已经存在一个名为 content 的物理文件夹,并且扩展为站点的根目录。

如果您尝试浏览到内容文件夹,IIS 将给出该错误。请求不会到达路由 table.

考虑重命名文件夹或控制器。根据您项目的要求,每个都会有自己的连锁反应。

转到项目 properties select web 选项卡更改特定页面