Asp.net MVC Route 类支持 URL 中任意位置的 catch-all 参数
我想得越多,我就越相信可以编写一个使用这些 URL 定义的自定义路由:
{var1}/{var2}/{var3}
Const/{var1}/{var2}
Const1/{var1}/Const2/{var2}
{var1}/{var2}/Const
以及在任何上部 URL 中的任何位置上最多一个贪婪参数,例如
{*var1}/{var2}/{var3}
{var1}/{*var2}/{var3}
{var1}/{var2}/{*var3}
有一个重要的限制。具有贪婪段的路由不能有任何可选部分。所有这些都是强制性。
示例
这是一个示例性请求
http://localhost/Show/Topic/SubTopic/SubSubTopic/123/This-is-an-example
这是 URL 路由定义
{action}/{*topicTree}/{id}/{title}
算法
在 GetRouteData()
中解析请求路由应该像这样工作:
- 将请求拆分为段:
- 显示
- 主题
- 子主题
- 子子主题
- 123
- 这是一个例子
- 从左侧开始处理路由 URL 定义,并将单个段值分配给参数(或将请求段值与静态路由常量段匹配)。
- 当路由段定义为贪婪时,反向解析并转到最后一段。
- 向后一一解析路由段(为它们分配请求值),直到再次遇到贪婪的包罗万象的情况。
- 当您再次到达贪婪请求段时,连接所有剩余的请求段(按原始顺序)并将它们分配给贪婪的 catch-all 路由参数。
问题
据我所知,它是可行的。但我想知道:
- 是否有人已经写过这个,所以我不必这样做(因为解析还有其他方面我没有提到(约束、默认值等)。
- 你看到这个算法有什么缺陷吗? 我将不得不自己编写它。
,因为如果到目前为止我还没有考虑过 GetVirtuaPath() 方法,那么
the more I think about it the more I believe it's possible to write a custom route that would consume these URL definitions:
{var1}/{var2}/{var3}
Const/{var1}/{var2}
Const1/{var1}/Const2/{var2}
{var1}/{var2}/Const
as well as having at most one greedy parameter on any position within any of the upper URLs like
{*var1}/{var2}/{var3}
{var1}/{*var2}/{var3}
{var1}/{var2}/{*var3}
There is one important constraint. Routes with greedy segment can't have any optional parts. All of them are mandatory.
Example
This is an exemplary request
http://localhost/Show/Topic/SubTopic/SubSubTopic/123/This-is-an-example
This is URL route definition
{action}/{*topicTree}/{id}/{title}
Algorithm
Parsing request route inside GetRouteData()
should work like this:
- Split request into segments:
- Show
- Topic
- SubTopic
- SubSubTopic
- 123
- This-is-an-example
- Process route URL definition starting from the left and assigning single segment values to parameters (or matching request segment values to static route constant segments).
- When route segment is defined as greedy, reverse parsing and go to the last segment.
- Parse route segments one by one backwards (assigning them request values) until you get to the greedy catch-all one again.
- When you reach the greedy one again, join all remaining request segments (in original order) and assign them to the greedy catch-all route parameter.
Questions
As far as I can think of this, it could work. But I would like to know:
- Has anyone already written this so I don't have to (because there are other aspects to parsing as well that I didn't mention (constraints, defaults etc.)
- Do you see any flaws in this algorithm, because I'm going to have to write it myself if noone has done it so far.
I haven't thought about GetVirtuaPath()
method at all.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最近我问的问题很紧急,所以我通常会自己解决问题。抱歉,但这是我对我所询问的路线的看法。任何人发现任何问题:请告诉我。
在 URL 中的任何位置使用包罗万象的段进行路由
以及在上面的代码中使用的另外两个类:
这
就是全部了。如有任何问题请告诉我。
我还写了一篇博客文章 与此自定义路由类相关。它非常详细地解释了一切。
Lately I'm asking questions in urgence, so I usually solve problems on my own. Sorry for that, but here's my take on the kind of route I was asking about. Anyone finds any problems with it: let me know.
Route with catch-all segment anywhere in the URL
And additional two classes that're used within upper code:
and
That's all folks. Let me know of any issues.
I've also written a blog post related to this custom route class. It explains everything into great detail.
出色地。它不能位于默认层次结构中。因为,路由层从操作中分离出来。您无法操作参数绑定。您必须编写新的 ActionInvoker 或必须使用 RegEx 进行捕获。
Global.asax:
控制器:
Well. It cannot be in default hierarchy. 'cause, Routing layer splitted from actions. You cannot manipulate parameter bindings. You have to write new ActionInvoker or have to use RegEx for catching.
Global.asax:
Controller: