ASP.NET MVC路由帮助

时间:2011-05-02 18:07:42

标签: asp.net-mvc-3 routing

我有以下网址:

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

以下地图:

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})

虽然这似乎有效,但我需要一种方法来处理带有404的未知路由/网址。

我在这里看了几个样本,但似乎不能让它们起作用。

另外,这些路线看起来是否合适?我是在正确的轨道上吗?

由于

1 个答案:

答案 0 :(得分:3)

你走在正确的轨道上 - 看到这个帖子来处理所有404s。

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

特别是这部分

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