21

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.html so I know it will be served and exists but if I use www.mydomain.com I get a 404.

I have ensured that the default document is correctly set in IIS7 and I have even gone so far as to commented out all the routes defined in my global.asax to ensure I don't have a route causing this problem.

So to summarize:

  • I have an index.html file on disk and IIS7 is set to use index.html as the default document.
  • If I remove my ASP.NET MVC application and leave the index.html file is served as the default document as expected.
  • If I publish my ASP.NET MVC application then the index.html doesn't get served by default document.

Does anyone know how to get ASP.NET MVC to serve the default document?

Jon Cahill
  • 4,968
  • 4
  • 34
  • 31
  • perhaps you'd get a better response on serverfault.com – tzenes Mar 11 '10 at 04:47
  • This is not an IIS7 issue it is an ASP.NET MVC issue. The default document gets served perfectly when not using ASP.NET MVC – Jon Cahill Mar 12 '10 at 02:24
  • I'm actually confused why this would need to be done – Alastair Pitts Mar 12 '10 at 02:33
  • 1
    Pretty simple reason why I want/need to do this. I need to add some functionality to an existing site which is made up of static html files. All I want to do is add functionality using ASP.NET MVC to one area of the site but leave the remaining as it is. – Jon Cahill Mar 15 '10 at 09:31
  • It should be possible to override the default page without having to recompile/redeploy. I just want to put a 'under maintenance' page up for a little while... This should be possible for an Site Admin to do without needing a dev?? – AndyM May 28 '13 at 12:33

6 Answers6

29

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("");
pius
  • 2,344
  • 1
  • 23
  • 25
  • "Therefore the routing looks for a controller with that name. In other words, it looks for a controller with no name at all." -- actually, it's looking for Home/Index -- those are the parameter defaults in the MapRoute call – Mark Sowul Mar 22 '17 at 19:27
2

Sorry for resurrecting this mummy, but i don't believe this issue was ever a default document issue. In fact you probably don't want to have a default document set as many of the other answerers have stated.

Had this problem as well, a similar problem. the cause of my issue was that the Application Pool for the site was set to use .NET Framework v2 and should have been set to v4. once I changed that it loaded correctly.

Mabdullah
  • 1,013
  • 6
  • 26
2

I found a way around this. If you want index.html to be in the root of your MVC application (i.e next to your controller/model/view/appdata etc folders), you can do this:

Say you have home.html, aboutus.html and contactus.html.

//this route matches (http://mydomain.com/somekindofstring)

routes.MapRoute(
    "SingleRootArg",
    "{id}",
    new { controller = "Home", action = "Details", id=""});

// so now that you have your route for all those random strings. 
// I had to do this for vanity urls. mydomain.com/ronburgandy  etc. however, 
// mydomain.com/index.html will also come in just how you'd expect. 

//Just an example of what you COULD do. the user would see it as root html. 

Public ActionResult Details(string id)
{

    //let me save you the trouble - favicon really comes in as a string *ANGER*
    if(String.IsNullOrEmpty(id) || id.ToLower().Contains("favicon.ico"))
        return Redirect("~/index.html");
    if(id == "aboutus.html")
        return Redirect("~/aboutus.html");
    if(id == "contactus.html")
        return Redirect("~/contactus.html");
    if(id == "index.html")
        return Redirect("~/index.html");
}

index.html aboutus.html index.html are now at the same level as my CSPROJ file.

Jonas G. Drange
  • 8,749
  • 2
  • 27
  • 38
user398753
  • 21
  • 2
2

I had a similar problem with a WebForms application. In your web.config make sure the resourceType attribute of the StaticFile handler under system.webServer is set to Either.

<add name="StaticFile" path="*" verb="*" type="" modules="StaticFileModule,DefaultDocumentModule,DirectoryListingModule" scriptProcessor="" resourceType="Either" ...
Luke Smith
  • 23,504
  • 8
  • 29
  • 28
  • The `routes.IgnoreRoute("");` method didn't work for me. This did. Thanks. – Colin Young Jan 25 '13 at 14:57
  • I was able to use this with Orchard to get Default Document working. Adding DefaultDocumentModule and having resourceType="Either" was the key. – James Jul 29 '14 at 10:29
1

You could ignore the route in your MVC application and let IIS serve it.

routes.IgnoreRoute("index.html")
etc
tarn
  • 2,192
  • 2
  • 13
  • 17
  • 3
    Unfortunately this doesn't work for a "Default document" because the user is not typing index.html into the address. If you go directly to index.html, this works. – mkchandler Sep 10 '10 at 14:38
  • @mc2thaH You will need to change the "default document" setting in IIS from default.aspx to index.html. The OP said he did this. – tarn Sep 13 '10 at 01:33
  • 1
    mkchandler is right, even with the default document set, it didn't work for me. Instead I had to add routes.IgnoreRoute("") – pius Jul 01 '11 at 12:11
-3

I suspect you added index.html yourself as that extension would be unknown to the mvc framework.

Your default index page in mvc is //domain/home/index and is physically index.aspx.

If you call your domain using //domain then the routing engine will assume /home/index.aspx and not index.html.

griegs
  • 22,624
  • 33
  • 128
  • 205
  • The routing engine will not be routing to the index action on the home controller because as I said I have commented out all the route in my global.asax. There is an index.html file on disk and IIS7 is set to use index.html as the default document but ASP.NET MVC is preventing this somehow – Jon Cahill Mar 12 '10 at 02:16