Michel
Michel

Reputation: 23655

throw 404 error when route is not satisfied

using the default route

  routes.MapRoute(
      "Admin",                                              // Route name
      "Admin/{action}",                           // URL with parameters
      new { controller = "Admin", action = "Index" }  // Parameter defaults
  );

lots of routes (approx. 90%) for our application are working, so that's fine.

Now when the user enters /logon/strangeroute, MVC offcourse throws an error because i don't have the strangeroute action in my logoncontroller.

Using the catch all exception handler in the global.asax.cs, i can't find to get the difference between routing errors (i call this a routing error) and another error (thrown in the rest of the code).

I would like to discriminate betweek the types of errors because when the route gives an error it most of the time is a 404 error, and i want to redirect the user to the 404 page and not to the normal, generic, error page.

One soultion would be to create all the routes for all the pages and dismiss the existing default route and implement a catch-all route but i'd prefer not to create a separate route for every url.

Is there another way of doing this?

Upvotes: 1

Views: 562

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1039598

Test if the Exception you caught is an HttpException and if it is use the GetHttpCode method on it to see if it is 404.

Upvotes: 1

Related Questions