Sam
Sam

Reputation: 15770

ASP.NET MVC Routing Help

I have the following url(s):

http://localhost/About
http://localhost/Contact
http://localhost/Shop
http://localhost/Shop/ListProducts/{CategoryID}/{CategoryName}
http://localhost/Shop/Checkout
http://localhost/Customer
http://localhost/Customer/Edit
http://localhost/Customer/ChangePassword
http://localhost/Authentication/LogOn
http://localhost/Authentication/LogOff

And the following maps:

routes.MapRoute("Authentication", "Authentication/{action}", New With {.controller = "Authentication", .action = "Index"})
routes.MapRoute("Customer", "Customer/{action}", New With {.controller = "Customer", .action = "Index"})
routes.MapRoute("Shop", "Shop/{action}/{CategoryID}/{CategoryName}", New With {.controller = "Shop", .action = "Index", .CategoryID = UrlParameter.Optional, .CategoryName = UrlParameter.Optional})
routes.MapRoute("Main", "{action}", New With {.controller = "Main", .action = "Index"})
routes.MapRoute("Default", "{controller}/{action}/{id}", New With {.controller = "Main", .action = "Index", .id = UrlParameter.Optional})

While this seems to work, I need a way to handle unknown routes/urls with a 404.

I have looked at a few samples here, and cant' seem to make them work.

Also, do these routes look right? Am I on the right track?

Thanks

Upvotes: 1

Views: 210

Answers (1)

Adam Tuliper
Adam Tuliper

Reputation: 30152

You are on the right track - see this posting to handle ALL 404s.

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

specifically this part

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

Upvotes: 3

Related Questions