发送多个查询到的WebAPI控制器 [英] Sending multiple queries to a webapi controller

查看:152
本文介绍了发送多个查询到的WebAPI控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在的WebAPI(除了ODATA)来指定多个查询?
下面是这种情况:
自定义存储过程。接受2拉链codeS,进行距离计算并返回的距离
该端点的WebAPI接受拉链并调用存储过程的。并返回一个DTO的距离
类ZIPDistance
{
  公共字符串ZIP1,
  公共字符串ZIP2,
  公共sttring距离
}

Is there a way in webapi(besides odata) to specify multiple queries? Here is the scenario: A custom sproc. that accepts 2 zip codes, performs distance calculation and return the distance The WebAPI endpoint accepts the zips and calls into the sproc. and returns a Distance DTO class ZIPDistance { public string Zip1, public string Zip2, public sttring Distance }

这样做的URL:/ API / ZipDistances $选择=距离和放大器; $ =过滤器EQ ZIP1和13240 EQ ZIP2 90210?
在ZipDistancesController我们提取使用ODataQueryOptions从上面的查询参数,调用到存储过程。
返回到上面的DTO。

The URL for this: "/api/ZipDistances?$select=Distance&$filter=Zip1 eq 13240 and Zip2 eq 90210 In the ZipDistancesController we extract the parameters from the above query using ODataQueryOptions, call into the sproc. and return the DTO above.

有关潜在性能的原因,被请求的上述终端以某种方式接受拉链code,即对集合的集合
在ZIPDistance的DTO的重新presenting的为此我们需要上述调入存储过程,遍历所有的DTO的,调用存储过程的拉链的对的。每DTO并返回的DTO的集合的结果
让客户将使得单一HTTP调用而不是多个电话,获得1个呼叫所有结果。

For potential performance reasons it was requested that the above endpoint somehow accept a collection of zip code pairs i.e. a collection of the "ZIPDistance" DTOs representing the "pairs" of zips for which we need to call into the sproc above, loop over the DTOs, invoke the sproc. per DTO and return a collection of DTOs for the result so that a client would make a single HTTP call instead of multiple calls and obtain all results in 1 call.

这是我知道这样做的唯一方法是通过POST并通过ZIPDistances的集合中重新presenting中的zip code对
有效载荷,但是这是从根本上对REST,因为现在我们改变POST的语义意味着数据检索(即GET)

The only way that I am aware of doing is via a POST and pass in the collection of ZIPDistances representing the zip code pairs in the payload, but this is fundamentally against REST since now we change the semantic meaning of POST to mean data retrieval (i.e. GET)

问题是由的WebAPI,什么支持上述方案是否会实现上述不超载的最佳方式
动词意义和造成混乱。

The question is whether the above scenario supported by WEBAPI and what would be the best way for implementing the above without overloading verb meaning and causing confusion.

更新:

我已经出原型一种可能的解决方案这里其中涉及在URL中嵌入对:

I've prototyped out one possible solution here which involves embedding the pairs in the URL:

http://<host>/api/ZipDistances?$select=Distance&$filter=Pairs eq 'Zip1 eq 13240 and Zip2 eq 90210,Zip1 eq 13241 and Zip2 eq 90211'

和相应的DTO:

 public class ZipDistanceDTO
    {
        public string ZipPairs { get; set; }
        public string Distance { get; set; }
    }

这将返回以下结果:

[{"ZipPairs":"'Zip1 eq 13240 and Zip2 eq 90210","Distance":"558"},
{"ZipPairs":"Zip1 eq 13241 and Zip2 eq 90211'","Distance":"558"}]

在这个

评论/想法,将AP preciated。

Comments/Thoughts on this would be appreciated.

UPDATE(2):
我已经发布了另一个原型这里了使用查询资源

UPDATE (2): I've posted another prototype here that uses a Query resource

推荐答案

发出带有多个ZIP codeS适当的GET请求是蛮好用。

Issuing proper GET request with multiple ZIP codes is just fine to be used.

不过,

另一种可能的REST风格的方法是创建一个使用POST一个查询资源的对象,返回查询资源ID号,这将在后面分离的GET请求使用。

However,
Another possible RESTfull way is to create a "Query Resource" object using POST, returning "Query Resource ID Number", which will later be used in a separated GET request.

通过正在创建保存查询,也可以高效地为重新查询也是如此。

By that you are creating saved queries which can also be efficient for re-querying as well.

这篇关于发送多个查询到的WebAPI控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆