If you’re using ASP.NET Routing (without the MVC abstractions), you’ll be trying to route http://www.mysite.com/Default.aspx sooner or later.

This won’t work:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.Add("Default", new Route("Default.aspx", new RouteHandler("~/Site/Default.aspx")));
}

It won’t work because the request isn’t actually for Default.aspx, the request is for the site root, which IIS will catch. IIS responds by attempting to serve the default page for the site (probably Default.aspx), which may or may not exist.

To route requests to the site root, you’ll need to do this instead:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.Add("Default", new Route(string.Empty, new RouteHandler("~/Site/Default.aspx")));
}

We’re now handling requests for the site root (which will be an empty string, as far as the Routing engine is concerned), so IIS and the site’s default page are never invoked.

You could use RouteExistingFiles = true, but I prefer this solution. It more explicitly defines what you’re trying to accomplish, which is always better for readability.

posted on Thursday, April 07, 2011 10:01 AM | Filed Under [ Development Microsoft ]

Comments

Gravatar
# re: ASP.NET Routing and Default.aspx (AndyKo @ 12/15/2011 5:50 AM)

very helpfull for me post. thank's a lot
 
Gravatar
# re: ASP.NET Routing and Default.aspx (Ali @ 3/29/2014 8:43 PM)

Thanks bro

Post Comment

Title *
Name *
Email
Url
Comment *  
Remember me
Please add 4 and 3 and type the answer here:

Search

Site Sections

Recent Posts

Archives

Post Categories

WHS Add-In Tutorial

WHS Blogs

WHS Development