Set index.html as the default page

后端 未结 5 706
闹比i
闹比i 2020-12-29 20:07

I have an empty ASP.NET application and I added an index.html file. I want to set the index.html as default page for the site.

I have tried to right click on the ind

相关标签:
5条回答
  • 2020-12-29 20:51

    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.

    0 讨论(0)
  • 2020-12-29 21:01

    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 }
            );
    
    0 讨论(0)
  • 2020-12-29 21:05

    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>
    
    0 讨论(0)
  • 2020-12-29 21:08

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

    routes.IgnoreRoute(""); 
    
    0 讨论(0)
  • 2020-12-29 21:14

    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.

    0 讨论(0)
提交回复
热议问题