【问题标题】:URL Routing Categories and Sub-CategoriesURL 路由类别和子类别
【发布时间】:2013-08-14 20:30:13
【问题描述】:

我已经搜索了很长时间来解决我的问题,但我没有找到一个..

我想,我不是唯一一个有这个任务并希望得到答案的人。

我的网站存储产品.. 很多产品。所有这些产品都有一个或多个类别(或子类别)。在正确的类别中检索产品不是问题。我将在产品名称的末尾添加此 ID,例如:http://localhost.com/products/details/books/romance/romeo-and-julia-3245。控制器是产品,动作是细节。

但是,如果我想列出一个类别的所有产品,那么获得干净 URL 的最佳解决方案是什么?问题是,一个子类别可能不止一次存在。例如:

http://localhost.com/products/list/games/romance

http://localhost.com/products/list/books/romance

如果我在产品控制器中调用列表操作,我无法检测到子类别“浪漫”是否与游戏或书籍相关。

我该如何解决这个问题?

最好的问候

【问题讨论】:

    标签: asp.net-mvc web url-rewriting url-routing


    【解决方案1】:

    您可以尝试为ActionResult List 编写2 个路由并使用参数:如果ActionLink 包含名为games 的参数,则它采用路由"Games",如果books - 它采用路由"Books"

    routes.MapRoute(
                    name: "Games",
                    url: "{controller}/{action}/games/romance",
                    defaults: new { controller = "products", action = "List", games = UrlParameter.Optional}
                );
    
    routes.MapRoute(
                    name: "Books",
                    url: "{controller}/{action}/books/romance",
                    defaults: new { controller = "products", action = "List", books = UrlParameter.Optional}
                );
    

    我不确定它是否符合您的需求,但您可以使用这个逻辑。

    【讨论】:

    • 嘿,谢谢你的评论。问题更多的是子类别。更多子类别可以具有相同名称的子类别。我需要识别类别,但带有 id 的解决方案不是 seo clean 对吗?
    猜你喜欢
    • 2013-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多