Set index.html as the default page
Asked Answered
A

5

31

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 index.html and set as start page, and when I run it the url is: http://localhost:5134/index.html but what I really want is that when I type: http://localhost:5134, it should load the index.html page.

my route config:

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

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }
    }
Alcaic answered 26/3, 2014 at 9:43 Comment(5)
At what controller is your action "index"? Is at "Home" controller?Prowl
i have only one controller in application : public class MainController : ControllerAlcaic
ok, so try change defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } to defaults: new { controller = "Main", action = "Index", id = UrlParameter.Optional }Prowl
I don't know if this is case sensitive, so becareful with action = "Index" if the really action name is 'index'Prowl
is this url "localhost:5134" still renders index page? If yes then you need to set start page in Solution>Properties>Web>Start Action> Specific PageAnemo
A
33

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

routes.IgnoreRoute(""); 
Alcaic answered 27/3, 2014 at 8:19 Comment(2)
The above answer worked locally for me but when I deployed it to production, I started getting method not allowed on Post requests.Dorina
it's weird because it works in iisexpress without this... any thoughts as to why it's different in IIS8 and requires this?Zeba
M
21

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.

Mort answered 19/1, 2015 at 14:4 Comment(1)
Thanks. This is only one that worked. Should be best answer. Why not use .cshtml mvc view for everything? Because other people work on websites other than .net developers with visual studio. Business designers prefer to maintain their own marketing html pages, as it works best doing it that way for everyone.Novelistic
D
8

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>
Distinguish answered 30/4, 2015 at 7:42 Comment(1)
In MVC, the other answers are required, but this is also required.Landin
R
6

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 }
        );
Rayerayfield answered 2/7, 2016 at 1:55 Comment(1)
this is a really bad idea. huge benefit of index.html is load speed. above will first hit the server, go through a whole server request cycle until it gets to that redirect, and then finally tell the browser to go load the .html causing unnecessary delay. ideally you want to configure your web server to default to index.html without hitting any .net resources until they can do something useful.Johny
H
3

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.

Heterophyllous answered 26/11, 2014 at 17:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.