1

I'm tying to enable the default routing in MVC.

I want every 404 request to redirect to DefaultController DefaultRout()

I found How can i make a catch all route to handle '404 page not found' queries for ASP.NET MVC?

But {*url} dosen't work i'm getting 404 and not redirecting to the default page.

My code:

public static void RegisterRoutes(RouteCollection routes)
{
  routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
  routes.IgnoreRoute("{resource}.aspx/{*pathInfo}");
  routes.IgnoreRoute("{resource}.ascx/{*pathInfo}");
  routes.IgnoreRoute("{resource}.ashx/{*pathInfo}");
  routes.IgnoreRoute("{resource}.gif/{*pathInfo}");

  //http://localhost:4775/BW/A/Tasks
  routes.MapRoute("Pages", "A/{controller}", new { controller = "Tasks", action = "InitPage" });

  routes.MapRoute(
    "404-PageNotFound",
    "{*url}",
    new { controller = "Default", action = "DefaultRout" }
  );
}

What am I missing?

Thanks

Rafael

Community
  • 1
  • 1
SirMoreno
  • 1,109
  • 1
  • 15
  • 34
  • The controller and action would need to point to an actual controller and action, rather than `Default` and `DefaultRout` respectively. – Dan Atkinson Jan 02 '11 at 16:23
  • just to piggy back on that @Dan my answer assumes you have a controller named 'defaultcontroller' with a 'defaultroute' method. I generally use a 'notfound' method in my 'errorcontroller' – Justin Soliz Jan 02 '11 at 16:58
  • I have a defaultcontroller with 'defaultroute' method but it never goes there. and I don't want to use customErrors. – SirMoreno Jan 02 '11 at 18:03

2 Answers2

6

Are you unable to use your web.config? I think this would be easier:

<customErrors mode="On" defaultRedirect="/error/default">
  <error statusCode="403" redirect="/error/restricted"/>
  <error statusCode="404" redirect="/Default/DefaultRoute"/>
  <error statusCode="500" redirect="/error/problem"/>
</customErrors>
Dan Atkinson
  • 11,391
  • 14
  • 81
  • 114
Justin Soliz
  • 2,781
  • 3
  • 25
  • 33
  • 1
    there is one little purpose when you shouldn't use this it's SEO. when you use web config with defaults client receive one page with 302 http header (redirect) and than page with 200. for improve crawlers need to return only one page with 404 header... – Vladimir Shmidt Apr 15 '12 at 00:05
0

There are a few things that I would like to point here.

I notice that you have used many IgnoreRoute entries for physical files. You don't have to do that as the framework looks for the physical files matching the url by default before routing it. You can disable the physical file matching by turning RouteExistingFiles to true on the RouteCollection in Global.asax. In this case you haven't done that.

Secondly, the way you have set it up, any route but /A/{controller} will be caught by the catch all route (anything starting with * is a catch all route) that you have configured.

I have tried this configuration and it does catch all the other routes except the one mentioned above. One thing you have to keep in mind, however, is that the above configuration will still matching everything with the following type of url: /A/something/ because the second segment will always match the {controller} placeholder. To only match this url with the "Tasks" controller, you can define a constraint on the route as the following:

routes.MapRoute("Pages", "A/{controller}", new { controller = "Tasks", action = "InitPage" }, new {controller="Home"});

There is also a spelling mistake in your catch all route configuration. action = "DefaultRout" should be action = "DefaultRoute"

Hope this helps.

Imran Rashid
  • 1,328
  • 14
  • 21