多个GET在网页API调用错误动作 [英] Multiple GET's in Web API calling wrong action

查看:158
本文介绍了多个GET在网页API调用错误动作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个的Web API ,看起来像下面...

I have a Web API, that looks like the following...

public class LeaguesController : ApiController
{
    //api/Leagues/active/1
    //api/Leagues/complete/1
    //api/Leagues/both/1
    [GET("api/Leagues/{type}/{id}")]
    public List<Competition> Get([FromUri]int id, 
                                [FromUri]CompetitionManager.MiniLeagueType type)
    {
        return CompetitionManager.GetUsersMiniLeagues(id, true, type);
    }

    //api/Leagues/GetMiniLeagueTable/3
    [GET("api/Leagues/GetMiniLeagueTable/{id}")]
    public List<SportTableRow> GetMiniLeagueTable([FromUri]int id)
    {
        return SportManager.GetMiniLeagueTable("", id).TableRows;
    }
}

在我所说的第一种方法获取,这工作正常。
当我使用小提琴手或Chrome REST客户端调用第二种方法 GetMiniLeagueTable ,我收到以下错误:

When I call the first method Get, this works fine. When I use fiddler or Chrome REST Client to call the second method GetMiniLeagueTable, I am getting the following error:

{消息:请求是无效的。 MessageDetail:参数
  字典包含不可为空的参数类型的空项
  键入'CompetitionManager + MiniLeagueType'的方法
  System.Collections.Generic.List`1 [竞争]获取(的Int32,
  MiniLeagueType)中的LeaguesController。一个可选参数必须是
  引用类型,可空类型,或声明为可选
  参数。}

{ Message: "The request is invalid." MessageDetail: "The parameters dictionary contains a null entry for parameter 'type' of non-nullable type 'CompetitionManager+MiniLeagueType' for method 'System.Collections.Generic.List`1[Competition] Get(Int32, MiniLeagueType)' in 'LeaguesController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter." }

我使用 AttributeRouting 装饰方法,但这似乎并没有工作。这是工作的罚款我之前介绍过 MiniLeagueType

能欣赏到这问题的人来了,或者你可以点我到我要去的地方错了?

Has anyone come across this issue, or can you point me to where I am going wrong?

推荐答案

我觉得原因是这个网址: API /联盟/ GetMiniLeagueTable / 3 。这两个URL匹配的路线,因为它可以跨preTED这样:<?code> API /联盟类型= GetMiniLeagueTable和ID = 3 。但它不能转换 GetMiniLeagueTable CompetitionManager.MiniLeagueType 值,所以它会产生错误。

I think that the cause is this url : api/Leagues/GetMiniLeagueTable/3. This url matches both routes, as it can be interpreted as such : api/Leagues?type=GetMiniLeagueTable&id=3. But it cannot convert GetMiniLeagueTable as a CompetitionManager.MiniLeagueType value, so it raises an error.

您应作出更具体的路线,例如 API /联盟/ GetCompetitions / {类型} / {ID} ,以prevent URL匹配2更不同的路线。

You should make more specific routes, for example api/Leagues/GetCompetitions/{type}/{id}, in order to prevent url matching 2 or more different routes.

另一种可能性是反转你的行动命令,因为它会才去为下一个,如果网址不符合检查的第一个行动的路线。

Another possibility is to invert your action orders, as it will check first action's route before going for the next one if the url doesn't match.

这篇关于多个GET在网页API调用错误动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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