对不同参数名网页API一条路由多个GET动词 [英] Multiple GET verb on single route with different parameter name Web Api

查看:382
本文介绍了对不同参数名网页API一条路由多个GET动词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

哪有可以调用使用单个路由参数名的基础不同,不同的操作。

我需要以下

  / API / V1 /用户
得到
关键= dfddg&安培;秘= fafassaf&安培;查询=选择的ID从用户那里用户LIKE'%GGG%

  / API / V1 /用户
得到
关键= dfddg&安培;秘= fafassaf&安培; IDS = fadfdafdsf,faffasfasfsf,asfasfasfasfas,asfasfasfasf

我已经写了下面的code

  [路线preFIX(API / V1 /用户)]
公共类UserController的:ApiController
{
    [得到()]
    公共字符串GetAllUsers(字符串键,弦乐的秘密,查询字符串)
    {
        返回GetAllUsers
    }    [得到()]
    公共字符串GetLookupUserIds(字符串键,弦乐的秘密,字符串ID)
    {
        返回GetLookupUserIds
    }

但第一种情况下工作正常,但第二个引发异常

  {
    消息:没有HTTP资源发现,请求URI相匹配的http://本地主机:14823 / API / V1 /用户密钥= rhdgsdgdsr&安培;秘= fhdgdgdfhdfh&安培; IDS = fdfdf,dfadfff?'。
    MessageDetail:没有采取任何控制器上的用户与请求匹配找到。
}


解决方案

我相信这里的问题是, API / V1 /用户的请求是由第一个匹配在路由表中的路由(注意:路线匹配首先发生在它不考虑查询参数,然后采取行动匹配发生)。现在,在路由表中的路由一号可以基于反射秩序上属性路由是增加的动作到它。 (您可以检查路由表 GlobalConfiguration.Configuration.Routes 项的样子)。

路由属性通过指定动作变量动作名称的价值增加了​​路线。网页API的行为选择的逻辑是,如果它看到那里的变量分配的的行动,它会尝试寻找与此同名的操作列表中的最佳匹配动作(动作超载情况像你这样)。

您可以尝试以下操作:


  1. 相同的动作名称的通过使用上述两种方法 ActionName
    属性。


  2. 如果1没有意义,你很可能已经采取的行动不同的路线temmplate。


How can possible to call different-different action on basis of parameter name using single route.

I need following

/api/v1/user
GET
key=dfddg&secret=fafassaf&query=select id from user where user like '%ggg%'

and

/api/v1/user
GET
key=dfddg&secret=fafassaf&ids=fadfdafdsf,faffasfasfsf,asfasfasfasfas,asfasfasfasf

I have written following code

[RoutePrefix("api/v1/user")]
public class UserController : ApiController
{
    [GET("")]
    public String GetAllUsers(String key, String secret, String query)
    {
        return "GetAllUsers";
    }

    [GET("")]
    public String GetLookupUserIds(String key, String secret, String ids)
    {
        return "GetLookupUserIds";
    }

but first case working fine but second one throwing exception

{
    "Message": "No HTTP resource was found that matches the request URI 'http://localhost:14823/api/v1/user?key=rhdgsdgdsr&secret=fhdgdgdfhdfh&ids=fdfdf,dfadfff'.",
    "MessageDetail": "No action was found on the controller 'User' that matches the request."
}

解决方案

I believe the issue here is that a request for api/v1/user is matched by the 1st route in the route table.(Note: route matching happens first where it doesn't consider query parameters and then action matching happens) Now, the 1st route in the route table could be reflection order based on which attribute routing is adding the actions to it. (you can check how the route table GlobalConfiguration.Configuration.Routes entries look like).

Attribute routing adds routes by assigning action variable the value of the action name. Web API's action selection has logic where if it sees that the action variable is assigned, it will try to look for the best matching action among list of actions with this same name (action overloading scenario like yours).

You can try the following:

  1. Have same action name for both methods above by using ActionName attribute.

  2. if 1. doesn't make sense, you could probably have different route temmplate for the actions.

这篇关于对不同参数名网页API一条路由多个GET动词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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