Reputation: 1107
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
Upvotes: 1
Views: 2641
Reputation: 1338
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.
Upvotes: 0
Reputation: 2811
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>
Upvotes: 6