1

How can I set MapRoute from root to index.html file?

I would like to redirect http://localhost:61944/ to http://localhost:61944/index.html

I try:

routes.MapRoute(
            name: "Root",
            url: "index.html"
        );

Inside the RegisterRoutes, but it returned 404 (Not Found).

Thanks

NightOwl888
  • 55,572
  • 24
  • 139
  • 212
Max Boy
  • 317
  • 6
  • 21

4 Answers4

2

If you are using ASP.NET Core, you can enable static files serving:

Add UseStaticFiles() in Startup.cs

public void Configure(IApplicationBuilder app)
{
    ...
    app.UseMvcWithDefaultRoute();
    app.UseStaticFiles();
}

Your index.html need to to be in wwwroot folder

koryakinp
  • 3,989
  • 6
  • 26
  • 56
1

There are a couple of problems with this.

First of all, the default configuration in Web.config will deny any URL with a . character in it. If you did use routing for this, you would need to reconfigure the web.config.

That said, you specifically mentioned "file". MVC doesn't serve files, it serves resources (either content or streams). Most (if not all) web servers are already setup to serve files (especially html, css, and js files), so unless there is a specific reason you need to, there is no reason to involve ASP.NET/MVC in this at all. Depending on what web server you are using, you should consult the documentation on how to set up index.html as a default page.

If you decide you want MVC to handle the request rather than the web server, this route configuration won't work because it does not supply the required route values controller and action in order to actually route the URL to an MVC controller.

routes.MapRoute(
        name: "Root",
        url: "index.html",
        defaults: new { controller = "Home", action = "Index" }
    );

I should also point out that this answer applies to MVC 5 without OWIN. For future reference, it would be helpful if you specify or to indicate which version you are referring to.

NightOwl888
  • 55,572
  • 24
  • 139
  • 212
  • I see... I'm using MVC to build a small website. Should I use webforms? BTW, I notice that it takes too long (about 3 seconds) to load the controller method by an AJAX call. – Max Boy Jan 26 '18 at 21:33
  • Which technology to use is a subjective question, which of course depends on your requirements. That is something you need to research and evaluate on your own. As for why your AJAX call is taking too long, I suggest you post another question on StackOverflow about it - this most likely isn't related to the technology you are using so much as the specific code or configuration you are using. – NightOwl888 Jan 26 '18 at 21:38
1

The ASP.NET route mapper can route to files or controllers.

Try this example: (you will need to rename your .html file to .aspx, but you don't have to make any other changes to it.)

In your route config file:

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

        // route default URL to index.aspx
        routes.MapPageRoute(
            routeName: "DefaultToHTML",
            routeUrl: "",
            physicalFile: "~/index.aspx",
            checkPhysicalUrlAccess: false,
            defaults: new RouteValueDictionary(),
            constraints: new RouteValueDictionary { {  "placeholder", ""} }
        );

        // other routes go here...

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }
}
Mike Smith - MCT
  • 1,166
  • 1
  • 9
  • 15
0

What worked for me was what I found here. Just add this to RegisterRoutes (before your routes.MapRoute of course):

routes.IgnoreRoute("");
Andrew
  • 7,602
  • 2
  • 34
  • 42