与 Nancy 初次匹配后捕获所有 url 片段

发布于 2024-12-12 09:41:03 字数 666 浏览 0 评论 0原文

我想要一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

纸短情长 2024-12-19 09:41:03

对于第一个问题,您可以使用正则表达式和简单名称来定义捕获组。所以你只需定义一个包罗万象的正则表达式。
对于第二个,您只需要枚举 Request.Query 字典。

下面是一些在单个路由中演示这两者的代码。

public class CustomModule : NancyModule
{
    public CustomModule() {
        Get["/views/{view}/(?<all>.*)"] = Render;
    }

    private Response Render(dynamic parameters) {
        String result = "View: " + parameters.view + "<br/>";
        foreach (var other in ((string)parameters.all).Split('/'))
            result += other + "<br/>";

        foreach (var name in Request.Query)
            result += name + ": " + Request.Query[name] + "<br/>";

        return result;
    }
}

完成此操作后,您可以调用诸如 /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.

public class CustomModule : NancyModule
{
    public CustomModule() {
        Get["/views/{view}/(?<all>.*)"] = Render;
    }

    private Response Render(dynamic parameters) {
        String result = "View: " + parameters.view + "<br/>";
        foreach (var other in ((string)parameters.all).Split('/'))
            result += other + "<br/>";

        foreach (var name in Request.Query)
            result += name + ": " + Request.Query[name] + "<br/>";

        return result;
    }
}

With this in place you can call a URL such as /views/home/abc/def/ghi/?x=1&y=2 and get the output
View: home
abc
def
ghi
x: 1
y: 2

Note:
The foreach over Request.Query is support in v0.9+

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文