Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC Routing to start at html page

Tags:

asp.net-mvc

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?

like image 765
Marsharks Avatar asked Apr 27 '09 18:04

Marsharks


1 Answers

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.

like image 136
tvanfosson Avatar answered Sep 20 '22 14:09

tvanfosson