Jon Cahill
Jon Cahill

Reputation: 4988

ASP.NET MVC not serving default document

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:

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

Upvotes: 20

Views: 38887

Answers (6)

Mabdullah
Mabdullah

Reputation: 1013

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.

Upvotes: 2

user398753
user398753

Reputation: 21

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.

Upvotes: 2

pius
pius

Reputation: 2424

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("");

Upvotes: 28

Luke Smith
Luke Smith

Reputation: 24254

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" ...

Upvotes: 2

tarn
tarn

Reputation: 2182

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

routes.IgnoreRoute("index.html")
etc

Upvotes: 1

griegs
griegs

Reputation: 22760

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.

Upvotes: -3

Related Questions