如何在 MVC 中最好地实现多级路由
我有一个网站,我想要一个类似以下的 URL:
/Some maincategory name/{id}/Some subcategory name/{id}/Some item name/{id}
我包含每个级别的 ID因为这个名字并不唯一。这是可行的,但我必须为每个级别创建一个新的路由。我的 Html.ActionLink 看起来也很糟糕。
每个级别都有自己的控制器,因为级别完全不同。 URL 可能是这样的:
_/Birds/2/Waders/4/Flamingos/23_
_/Mammals/5/Dogs/23/Longeared/25/Somedog/76_
_/Insects/7/Spiders_
这只是一个示例,不是我要使用的。我的申请与动物无关。
有没有好的方法可以做到这一点,或者我应该使用标准路由?
I have a site where I would like an URL like:
/Some maincategory name/{id}/Some subcategory name/{id}/Some item name/{id}
I include the IDs of each level since the name is not unique. This is doable but I have to create a new routing for each level. My Html.ActionLink also looks nasty.
Each level has it's own controller since the levels are completely different. The URLs could be something like this:
_/Birds/2/Waders/4/Flamingos/23_
_/Mammals/5/Dogs/23/Longeared/25/Somedog/76_
_/Insects/7/Spiders_
This is just an example and not what I'm going to use. My applications has nothing to do with animals.
Is there a good way of doing this or should I use the standard routing instead?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有两种解决方案:
使用全局路由会导致
http://you.url.com/controller/action/{mid} /{sid}/{tid}
其中 mid = 主类别 ID
sid = 子类别 ID
tid = item id
结合使用路由和查询字符串
http://you.url.com/controller/action/{tid} ?mid={mid}&sid={sid}
我建议你使用第二种方法,那么你就没有聚集路线上的东西。希望这有帮助!
There's two solutions for this:
Using Global Routing which results in
http://you.url.com/controller/action/{mid}/{sid}/{tid}
where mid = main category id
sid = sub category id
tid = item id
Using the combination of Routing and Query String
http://you.url.com/controller/action/{tid}?mid={mid}&sid={sid}
I would suggest you use the second method, then you don't have to mass up with the route things. and hope this help!!
创建一个 xml 文件来描述每个节点的类别、值、url,然后通过循环这些 xml 节点来创建路由表怎么样?
这种 xml 还可以帮助您在页面上创建面包屑。
How about creating an xml file describing your categories, values, urls at each node and then creating a route table by looping through those xml nodes?
That kind of xml can also help you to create bread crumbs on your pages.