在 OpenRasta 中是否可以模式匹配多个键/值对?

发布于 2024-09-02 16:29:54 字数 1032 浏览 7 评论 0原文

OpenRasta 中是否可以有一个 Uri 模式,允许提交相同键的值数组并将其映射到接受查询参数数组的处理程序方法。

示例:返回集合中名为 Dave Smith 的所有联系人。

HTTP GET /contacts?filterBy=first&filterValue=Dave&filterBy=last&filterValue=Smith

配置为:

哪种语法最适合 Uri 字符串模式匹配? (欢迎建议)

ResourceSpace.Has.ResourcesOfType<List<ContactResource>>()
    .AtUri("/contacts")
    .And.AtUri("/contacts?filterBy[]={filterBy}[]&filterValue[]={fv}[]") // Option 1
    .And.AtUri("/contacts?filterBy={filterBy}[]&fv={fv}[]") // Option 2

将映射到以下处理程序方法:

public object Get(params Filter[] filters)
{
    /*
    create a Linq Expression based on the filters using dynamic linq
    query the repository using the Linq
    */

    return Query.All<Contact>().Where(c => c.First == "Dave" && c.Last == "Smith").ToResource()
}

其中 Filter 定义为

public class Filter
{
    public string FilterBy { get; set; }
    public string FilterValue { get; set; }
}

Is it possible in OpenRasta to have a Uri pattern that allows for an array of values of the same key to be submitted and mapped to a handler method accepting an array of the query parameters.

Example: Return all the contacts named Dave Smith from a collection.

HTTP GET /contacts?filterBy=first&filterValue=Dave&filterBy=last&filterValue=Smith

With a configuration of:

What syntax would be best for the Uri string pattern matching? (Suggestions welcome)

ResourceSpace.Has.ResourcesOfType<List<ContactResource>>()
    .AtUri("/contacts")
    .And.AtUri("/contacts?filterBy[]={filterBy}[]&filterValue[]={fv}[]") // Option 1
    .And.AtUri("/contacts?filterBy={filterBy}[]&fv={fv}[]") // Option 2

Would map to a Handler method of:

public object Get(params Filter[] filters)
{
    /*
    create a Linq Expression based on the filters using dynamic linq
    query the repository using the Linq
    */

    return Query.All<Contact>().Where(c => c.First == "Dave" && c.Last == "Smith").ToResource()
}

where Filter is defined by

public class Filter
{
    public string FilterBy { get; set; }
    public string FilterValue { get; set; }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

深爱不及久伴 2024-09-09 16:29:54

.AtUri("/contacts?filterBy={filterby}&filterValue={filterValue}") 应该很高兴地映射到

Post(string[] filterby, string[] filterValues)

应该以正确的方式处理,如果不是,那就是一个错误。

如果您想要更好一点的东西,您也可以使用对象语法:

<input name="Filter:0.FilterBy" />
<input name="Filter:1.FilterBy" />

并且有

Post(IEnuemrable<Filter> filter)

但是您可能需要为此使用 post,而不是 get。解决此问题的常用方法是对完全构建的 URI 执行 Post-Redirect-Get,这也更适合缓存

.AtUri("/contacts?filterBy={filterby}&filterValue={filterValue}") should happily map to

Post(string[] filterby, string[] filterValues)

That should get processed the correct way, if not it's a bug.

You can also use the object syntax if you want something a bit nicer:

<input name="Filter:0.FilterBy" />
<input name="Filter:1.FilterBy" />

and have

Post(IEnuemrable<Filter> filter)

But you will probably need to use a post for this, not a get. The usual way to solve this is to do a Post-Redirect-Get to a fully built URI, which is also nicer for caching

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