16

I am using IIS 6. I think my problem is that I don't know how to route to a non controller using the routes.MapRoute.

I have a url such as example.com and I want it to serve the index.htm page and not use the MVC. how do I set that up? In IIS, I have index.htm as my start document and my global.asax has the standard "default" routing, where it calls the Home/Index.

    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 = "" }  // Parameter defaults
        );

    }

I added this:

    protected void Application_BeginRequest(object sender, EventArgs e)
    {
        if (Context.Request.FilePath == "/") Context.RewritePath("index.htm");
    }

it works. But is this the best solution?

skaffman
  • 398,947
  • 96
  • 818
  • 769
Marsharks
  • 400
  • 2
  • 5
  • 12
  • I think it's interesting that you point out that you're running IIS6. I can't help but think that's going to be a key factor in the solution – Dan Esparza Apr 27 '09 at 19:09

4 Answers4

20

I added a dummy controller to use as the default controller when the root of the web site is specified. This controller has a single index action that does a redirect to the index.htm site at the root.

public class DocumentationController : Controller
{
    public ActionResult Index()
    {
        return Redirect( Url.Content( "~/index.htm" ) );
    }

}

Note that I'm using this a the documentation of an MVC-based REST web service. If you go to the root of the site, you get the documentation of the service instead of some default web service method.

tvanfosson
  • 524,688
  • 99
  • 697
  • 795
  • 1
    First acceptable answer I've seen after several bounces around SO. Thanks! – Chris Jaynes Mar 13 '12 at 20:53
  • 2
    This would cause an additional request to fetch the index.html – an phu Jul 03 '14 at 18:39
  • Good answer, and simple to implement. – Del Oct 05 '14 at 13:08
  • @anphu seconded, this is not a solution - this adds an additional redirect to the first page load, which will cause a noticeable delay for users on a slow connection and defeat the point of serving up a static file as the starting point. You'd be better off serving up a view from /Home/Index than redirecting to a static file. As per your answer `routes.IgnoreRoute("");` is the best solution here. – Keith Aug 03 '16 at 06:01
7

Configure the asp.net routing to ignore root ("/") requests and let IIS's "Default Document" ISAPI filter serve the static index.htm file

Add the following to the RegisterRoutes method.

routes.IgnoreRoute("");
A_Sk
  • 4,532
  • 3
  • 27
  • 51
an phu
  • 1,823
  • 1
  • 16
  • 10
2

The best solution is to remove the default Controller. You're running into this issue, because you're specifying both the default page and the default route without any parameters.

By just removing the controller = "Home" on the route defaults, the / won't match the route anymore and because no other route will satisfy, IIS will look into the default documents.

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    routes.MapRoute(
        "Default",                                              // Route name
        "{controller}/{action}/{id}",                           // URL with parameters
        new { action = "Index", id = "" }                       // Parameter defaults
    );
}
João Silva
  • 569
  • 3
  • 9
-1

routes.IgnoreRoute ?

Also, see this question: How to ignore route in asp.net forms url routing

Community
  • 1
  • 1
chakrit
  • 61,017
  • 25
  • 133
  • 162