ASP.NET MVC not serving default document

后端 未结 6 870
北恋
北恋 2020-12-29 06:07

I have an ASP.NET MVC application where the default page should be index.html which is an actual file on disk.

I can browse to the file using www.mydomain.com/index.

6条回答
  •  孤城傲影
    2020-12-29 06:47

    ASP.Net MVC routing is controlled by the Global.asax / Global.asax.cs files. The out-of-the-box routing looks like this:

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    
        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );
    }
    

    When no path is specified, ie. www.domain.tld/, the route is the empty string, "". Therefore the routing looks for a controller with that name. In other words, it looks for a controller with no name at all. When it finds no such controller it serves up a 404 NOT FOUND error page.

    To solve the problem, either map that path to something meaningful or else ignore that route entirely, passing control over to the index.html file:

    routes.IgnoreRoute("");
    

提交回复
热议问题