ASP.NET MVC 中的递归路由
我有一个表
Category
--------
Id
Name
ParentId (null)
“类别”可以无限递归,但可能最多可达 3 个级别,我希望我的 url 映射到类别。 因此,如果表格如下所示,
Id Name ParentId
1 Entertainment null
2 Sport 1
3 Football 2
我的网址将如下所示: http://Localhost/Entertainment/Sport/Football
类别不会经常更改,因此可能会可以对它们进行硬编码作为最后的手段,但我真的不想这样做。
I have a table
Category
--------
Id
Name
ParentId (null)
Categories can be infinitely recursive, but will probably max out at about 3 levels, i want my urls to map to the categories.
So if the table looked like this
Id Name ParentId
1 Entertainment null
2 Sport 1
3 Football 2
My url would look like:
http://Localhost/Entertainment/Sport/Football
Categories won’t change too often so it might be possible to hard code them as a last resort however I don’t really want to.
i found this answer
Routing an hiearchical path from DB with ASP.Net MVC
is this still the only way to do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,通配符参数是处理这个问题的最佳方法。您可以将其与检查给定路径有效性的路由约束结合起来,以便可以传递不是类别集的 URL 以供其他路由处理。
Yes, a wildcard parameter is the best way to handle this. You can combine this with a route constraint that checks the validity of the given path so URLs that aren't sets of categories can be passed on for handling by other routes.