在 OpenRasta 中是否可以模式匹配多个键/值对?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
.AtUri("/contacts?filterBy={filterby}&filterValue={filterValue}")
应该很高兴地映射到应该以正确的方式处理,如果不是,那就是一个错误。
如果您想要更好一点的东西,您也可以使用对象语法:
并且有
但是您可能需要为此使用 post,而不是 get。解决此问题的常用方法是对完全构建的 URI 执行 Post-Redirect-Get,这也更适合缓存
.AtUri("/contacts?filterBy={filterby}&filterValue={filterValue}")
should happily map toThat 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:
and have
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