ASP.NET MVC 5 传统路由

ASP.NET MVC 5 Traditional Routing

当我调试它时,产品和子类别 link 工作正常,但是类别向我显示了列表,但是当我单击其中一个以向我显示每个产品中的产品时,没有显示任何内容.

这是我的ProductsController.cs。

public ActionResult Index(string category, string subcategory, string search, string sortBy, int? page){... }

在 RouteConfig.cs 我有:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        name: "ProductsCreate",
        url: "Products/Create",
        defaults: new { controller = "Products", action = "Create" }
    );

    routes.MapRoute(
        name: "ProductsbySubCategorybyPage",
        url: "Products/{subcategory}/Page{page}",
        defaults: new { controller = "Products", action = "Index" }
    );

    routes.MapRoute(
        name: "ProductsbyCategorybyPage",
        url: "Products/{category}/Page{page}",
        defaults: new { controller = "Products", action = "Index" }
    );

    routes.MapRoute(
        name: "ProductsbyPage",
        url: "Products/Page{page}",
        defaults: new { controller = "Products", action = "Index" }
    );

    routes.MapRoute(
        name: "ProductsbySubCategory",
        url: "Products/{subcategory}",
        defaults: new { controller = "Products", action = "Index" }
    );

    routes.MapRoute(
        name: "ProductsbyCategory",
        url: "Products/{category}",
        defaults: new { controller = "Products", action = "Index" }
    );                       

    routes.MapRoute(
        name: "ProductsIndex",
        url: "Products",
        defaults: new { controller = "Products", action = "Index" }
    );

    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );
}

您的 ProductsbyCategorybyPageProductsbySubCategorybyPage 覆盖。 当 ASP.NET 试图解析传入的 URL 时,它将停止查找匹配,而 URL 和 Products/A/Page3 将通过 ProductsbySubCategorybyPage 路由传递。路由模块不知道您更喜欢 A 是什么,子类别还是类别。您需要重构 RegisterRoutes 方法以使用唯一的路由掩码。例如 Products/SubCategory/{subcategory}Products/Category/{category}