Web API路由冲突

时间:2015-04-24 22:18:57

标签: asp.net-mvc asp.net-web-api

仅供参考,我没有自定义路由,也没有使用MapHttpRoute。

我的Web API控制器如下:

[RoutePrefix("api/stat")]
public class StatController : ApiController

在那个控制器中我有这些方法:

  [Route("{statType}")]
    public StatState GetCurrentStat(string statType, UserInfo userInfo)

 [Route("getAllAccount")]
    public Dictionary<string, StatState> GetAllAccountCurrentStat(UserInfo userInfo)

   [Route("getScoreHistory")]
        public StatHistory GetStatAccountScoreHistory(string statType, UserInfo userInfo)

使用此网址进行HTTP GET调用:/api/stat/getAllAccount正确映射到:

 [Route("getAllAccount")]
    public Dictionary<string, StatState> GetAllAccountCurrentStat(UserInfo userInfo)

使用此网址进行HTTP GET调用:/api/stat/getScoreHistory 不正确映射到:

[Route("{statType}")]
    public StatState GetCurrentStat(string statType, UserInfo userInfo)

如何正确映射/ api / stat / getScoreHistory以及为什么/ api / stat / getAllAccount按照我的预期执行?

1 个答案:

答案 0 :(得分:1)

您的第三个端点的定义中存在statType参数但不在/api/stat/getScoreHistory网址中,这意味着您的三个端点中的第一个是最佳匹配。

您需要从上一个端点中删除无关的statType参数:

[Route("getScoreHistory")]
public StatHistory GetStatAccountScoreHistory(UserInfo userInfo)