views:

32

answers:

1

Hi,

Just started my first MVC 2.0 .net application. And I set up some default pages like:

/Loa/Register
/Loa/About

But when I request for /Loa/sdfqsdf (random string) I get the "The resource cannot be found." error, how can I redirect this non-existing action to a default action?

Like an "action not found" default action?

thx!

A: 

Using routes

You can define more than one route (which is also quite common in real-life MVC applications), because some routes have particular settings that differ from the default one. And especially if you want to do decent SEO.

routes.MapRoute(
    "DefaultRoute",
    "{controller}/{action}/{id}",
    new { controller = "Home", action = "Index", id = string.Empty },
    new { action = "Register|Index|About" } // route constraint that limits the actions that can be used with this route
);

routes.MapRoute(
    "InvalidRoutes"
    "{*dummy}",
    new { controller = "Home", action = "Nonexisting" }
);

If you'll add additional routes to our route table, just make sure the InvalidRoutes is defined as the last one.

Robert Koritnik
I didn't knew "{*dummy}" was possible, Thanks!
Hans
Anything's possible. For those parts that you want defaults, you can set them, for others that you don't you can easily omit them. In my example, `dummy` doesn't have a default and it will be set to anything you will be accessing.
Robert Koritnik