Alfero Chingono
Alfero Chingono

Reputation: 2663

ASP.NET Routing - GetRouteData does not work if path exists

I have a HttpModule which intercepts all requests and loads data from the database based on routing rules. However, I run into one problem all the time; GetRouteData only works if the path does not exist:

var routeData = RouteTable.Routes.GetRouteData(new HttpContextWrapper(HttpContext.Current));

Assuming a request comes in for the url http://localhost/contact, I get the correct routing data relating to that url if that path does not exist in the file system. The problem appears when I want to customize the page at that url which I do by creating an aspx page in the path ~/contact/default.aspx. Once I do that, GetRouteData return null.

I have even tried creating a new HttpContext object, but I still can not retrieve route data if the page exists.

Has anyone ever run into this problem? Is there a solution/workaround?

All help will be greatly appreciated.

Upvotes: 4

Views: 4096

Answers (1)

Jonathan Wood
Jonathan Wood

Reputation: 67193

Set RouteCollection.RouteExistingFiles to true.

public static void RegisterRoutes(RouteCollection routes)
{
    // Cause paths to be routed even if they exists physically
    routes.RouteExistingFiles = true;

    // Map routes
    routes.MapPageRoute("...", "...", "...");
}

Beware though. IIS7 behaves a little differently than the server used when debugging within Visual Studio. I got bit by this when I deployed my application to the web. Check out this feedback I submitted to Microsoft Connection.

Upvotes: 3

Related Questions