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 }
            );
        }
    }
Robert MacLean
  • 38,975
  • 25
  • 98
  • 152
vir
  • 1,081
  • 4
  • 19
  • 31
  • At what controller is your action "index"? Is at "Home" controller? – Ninita Mar 26 '14 at 09:50
  • i have only one controller in application : public class MainController : Controller – vir Mar 26 '14 at 09:52
  • ok, so try change `defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }` to `defaults: new { controller = "Main", action = "Index", id = UrlParameter.Optional }` – Ninita Mar 26 '14 at 10:08
  • I don't know if this is case sensitive, so becareful with `action = "Index"` if the really action name is 'index' – Ninita Mar 26 '14 at 10:11
  • is this url "http://localhost:5134" still renders index page? If yes then you need to set start page in Solution>Properties>Web>Start Action> Specific Page – Ashwini Verma Mar 26 '14 at 10:24

5 Answers5

33

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

routes.IgnoreRoute(""); 
Robert MacLean
  • 38,975
  • 25
  • 98
  • 152
vir
  • 1,081
  • 4
  • 19
  • 31
  • 2
    Thank you! Two hours wasted on this one. – mxmissile Aug 17 '16 at 15:31
  • The above answer worked locally for me but when I deployed it to production, I started getting method not allowed on Post requests. – Ali Feb 08 '18 at 11:06
  • it's weird because it works in iisexpress without this... any thoughts as to why it's different in IIS8 and requires this? – JDPeckham Jun 21 '18 at 16:36
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.

Alex Sanséau
  • 8,250
  • 5
  • 20
  • 25
  • Thanks for the detail! Nicely explained. – Derek Morrison May 13 '15 at 18:56
  • 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. – RandallTo Sep 19 '15 at 14:51
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>
Boris Lipschitz
  • 9,236
  • 5
  • 53
  • 63
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 }
        );
vickey1611
  • 79
  • 1
  • 1
  • 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. – Sonic Soul Apr 07 '17 at 15:15
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.

sergio
  • 619
  • 6
  • 16