I have the following routes defined in an asp.net mvc project
//Products/Category/SubCategory/Page
routes.MapRoute(
"ProductCategoryTypePaging",
"Products/{Category}/{subCategory}/Page{page}",
new { controller = "Products", action = "Index" }, new { page = @"\d+" }
);
//Products/Category/Page
routes.MapRoute(
"ProductCategoryPaging",
"Products/{Category}/Page{page}",
new { controller = "Products", action = "Index" }, new { page = @"\d+" }
);
//Products/Category/SubCategory
routes.MapRoute(
"ProductCategoryType",
"Products/{Category}/{subCategory}",
new { controller = "Products", action = "Index", page = 1 }
);
//Products/Category
routes.MapRoute(
"ProductCategory",
"Products/{Category}",
new { controller = "Products", action = "Index" }
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
These seem to work fine BUT if i try and view product details, it breaks. To view details, the Url is like this:
/Products/Details/18
Running that matches on the Products/Category/SubCategory route
What do i need to do differently to make this work?
There are a number of ways to do this:
The fundamental problem is that the route structure in this app is ambiguous. What would you expect to happen if there was a category called "Details" that had a sub-category called "18"? Which route should it map?
In the vein of option #2 I list above, the constraint you could add to the category/subcategory route would be for example a RegEx constraint that disallows the category name to be "Details". That will cause the route to be skipped, but it will also prevent the app from ever having a category called "Details". If that's OK for the app, it should work just fine.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.