与 Nancy 初次匹配后捕获所有 url 片段
我想要一个 nancy 规则来匹配/捕获初始匹配后的所有 url 段。
例如,我想这样做:
有一个像这样的url:/views/viewname/pageid/visitid/someother
和这样的规则:
Get["/views/{view}/{all other values}"] = parameters =>
{
string view = parameters.view;
List<string> listOfOtherValues = all other parameters..
return ...
};
listOfOtherValues最终会是:
- pageid
- accessid
- someother
我也想对查询字符串参数执行此操作。
给定一个像这样的 url: /views/viewname?pageid=1&visitid=34&someother=hello
那么 listOfOtherValues 最终会是:
- 1
- 34
- hello
这对 Nancy 来说可能吗?
I would like to have nancy rule that matches/captures all url segments after the initial match.
For example I would like to do this:
have a url like: /views/viewname/pageid/visitid/someother
and a rule like this:
Get["/views/{view}/{all other values}"] = parameters =>
{
string view = parameters.view;
List<string> listOfOtherValues = all other parameters..
return ...
};
listOfOtherValues would end up being:
- pageid
- visitid
- someother
I would also like to do this for querystring parameters.
given the a url like: /views/viewname?pageid=1&visitid=34&someother=hello
then listOfOtherValues would end up being:
- 1
- 34
- hello
Is this even possible with Nancy?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于第一个问题,您可以使用正则表达式和简单名称来定义捕获组。所以你只需定义一个包罗万象的正则表达式。
对于第二个,您只需要枚举 Request.Query 字典。
下面是一些在单个路由中演示这两者的代码。
完成此操作后,您可以调用诸如
/views/home/abc/def/ghi/?x=1&y=2
之类的 URL 并获取输出查看:主页
abc
def
ghi
x:1
y: 2
注意:
v0.9+ 支持 Request.Query 上的
foreach
For your first problem you can use regular expression as well as simple names to define your capture groups. So you just define a catch all RegEx.
For your second, you just need to enumerate through the Request.Query dictionary.
Here is some code that demonstrates both in a single route.
With this in place you can call a URL such as
/views/home/abc/def/ghi/?x=1&y=2
and get the outputView: home
abc
def
ghi
x: 1
y: 2
Note:
The
foreach
over Request.Query is support in v0.9+