tags:

views:

343

answers:

3

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?

A: 

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
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
A: 

Hey there griegs, I found a way around this just now. 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, 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");

}

//yes this really works. index.html aboutus.html index.html are at the same level as my CSPROJ file lol

Good luck!

+1  A: 

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

routes.IgnoreRoute("index.html")
etc
tarn
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.
mc2thaH
@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