Set index.html as the default page

一世执手 提交于 2019-11-30 04:24:31
vir

I added an instruction to my route config to ignore empty routes and that solved my problem.

routes.IgnoreRoute(""); 

As @vir answered, add routes.IgnoreRoute(""); to RegisterRoutes(RouteCollection routes) which you should find in RouteConfig.cs by default.

Here's what the method could look like:

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

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

The reason is that ASP.NET MVC takes over URL management and by default the routing is such that all extensionless URLs are controlled by the extensionless Url handler defined in web.config.

There's a detailed explanation here.

Assuming the web app is running in IIS, the default page can be specified in a web.config file:

<system.webServer>
    <defaultDocument>
        <files>
            <clear />
            <add value="index.html" />
        </files>
    </defaultDocument>
</system.webServer>

Create a new controller DefaultController. In index action, i wrote one line redirect:

return Redirect("~/index.html")

In RouteConfig.cs, change controller="Default" for the route.

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

One solution is this one:

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

I mean, comment or delete this code in your MVC project to avoid the default behavior when you make the initial request http://localhost:5134/.

The index.html must be in the root of your solution.

Hope this helps! It works for me.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!