views:

38

answers:

4

In trying to get my application to produce 404 errors correctly, I have implemented a catch all route at the end of my route table, as shown below:

 routes.MapRoute(
            "NotFound", _
           "{*url}", _
           New With {.controller = "Error", .action = "PageNotFound"} _
       )

However, to get this working, I had to remove the default route:

{controller}/action/{id}

But now that the default has been removed, most of my action links no longer work, and the only way I have found to get them working again is to add individual routes for each controller/action.

Is there a simpler way of doing this, rather than adding a route for each controller/action?

Is it possible to create a default route that still allows the catch all route to work if the user tries to navigate to an unknown route?

A: 

You are probably better off configuring the 404 error document in the section of the configuration then restoring the default route.

FWIW, I think the default route requirement is retarded too.

Wyatt Barnett
A: 
//this catches all  requests
routes.MapRoute(
    "Error",
    "{*.}",
     new { controller = "PublicDisplay", action = "Error404" } 
);

add this route at the end the routes table

Praveen Prasad
A: 

Well, what I have found is that there is no good way to do this. I have set the redirectMode property of the customErrors to ResponseRewrite.

<customErrors mode="On" defaultRedirect="~/Shared/Error" redirectMode="ResponseRewrite">
    <error statusCode="404" redirect="~/Shared/PageNotFound"/>
</customErrors>

This gives me the sought after behavior, but does not display the formatted page.

To me this is poorly done, as far as SEO goes. However, I feel there is a solution that I am missing as SO does exactly what I want to happen. The URL remains on the failed page and throws a 404. Inspect stackoverflow.com/fail in Firebug.

Dustin Laine
Funny you should say that, when I first started looking at the issue, I first checked to see how SO handles it. I could manually add routes for each action, but I think this would be hard to maintain over time. I really think that MS could provide better guidance on the issue.
Sean Taylor
Maybe if we ask Jeff nicely he will share it with us? :)
Sean Taylor
After much digging around, trying out various solutions, this one seems to work ok for me. http://stackoverflow.com/questions/619895/how-can-i-properly-handle-404s-in-asp-net-mvc/2577095#2577095
Sean Taylor
A: 

Ah, the problem is your default route catches all 3 segment URLs. The issue here is that Routing runs way before we determine who is going to handle the request. Thus any three segment URL will match the default route, even if it ends up later that there's no controller to handle it.

One thing you can do is on your controller override the HandleMissingAction method. You should also use the tag to catch all 404 issues.

Haacked