我在asp.net mvc项目中定义了以下路由
//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
);
这些似乎工作正常,但如果我尝试查看产品详细信息,它会中断。要查看详细信息,Url是这样的:
/ Products / Details / 18
在Products / Category / SubCategory路线上运行匹配
我需要做些什么来使这项工作?
答案 0 :(得分:0)
有很多方法可以做到这一点:
根本问题在于此应用程序中的路径结构不明确。如果有一个名为“详细信息”的类别有一个名为“18”的子类别,您会发生什么?应该映射哪条路线?
在上面列出的选项#2的静脉中,您可以添加到类别/子类别路径的约束将是例如不允许类别名称为“详细信息”的RegEx约束。这将导致跳过路线,但它也会阻止应用程序出现名为“详细信息”的类别。如果这对应用程序没问题,它应该可以正常工作。