views:

225

answers:

2

I define a lot of explicit routes. One of them is:

routes.MapRoute("default", "",
  new { controller = "Home", action = "Index" });

At the end, I define a catchall route:

routes.MapRoute("PageNotFound", "{*url}",
  new { controller = "Error", action = "Http404" });

If I go to the homepage http://localhost, then the http404 page is shown. And strangely, if I remove the catchall route, then the welcome page appears correctly. Note also that I have a menu where I call Url.RouteUrl("default") and the link to the homepage is correctly generated.

So, why is my default route not activated when the catchall route exists?

Update: I'm using routes.RouteExistingFiles=true. If I remove it, then it works as expected. But I need it to be set to true. What's the problem here?

Thanks.

A: 

If you use "routes.RouteExistingFiles=true" it means it will route existing (physically exist) files as its own - so routing will be skipped for those. I think in your root website there is probably a "default.aspx" or "index.htm" or something like that.

Turning on RouteExistingFiles will then allow those files to be executed normally (instead of via routing).

Now I think what happen is that your catchall routing is overriding you RouteExistingFiles - so it automatically routes the default.aspx into your 404 catchall.

Johannes Setiabudi
A: 

If you still have the default route (I.E. {controller}/{action}/{id}) in RegisterRoutes() it will trap all URLs that match the format of a normal MVC request.

In other words the catch-all route can only intercept a bad URL if it doesn't fit the normal format (blah/blah/blah/blah).

In the case of a non-existent controller the exception must be handled through conventional ASP.NET handling.

Theres a good description of handling this here

RonnBlack