C# .net MapRoute to html file

5k Views Asked by At

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

4

There are 4 best solutions below

0
Mike Smith - MCT On BEST ANSWER

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 }
        );
    }
}
2
NightOwl888 On

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.

0
koryakinp On

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

0
Andrew On

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

routes.IgnoreRoute("");