【问题标题】:POST action taking JSON and returning JSON with ASP.NETPOST 操作采用 JSON 并使用 ASP.NET 返回 JSON
【发布时间】:2014-11-21 10:23:50
【问题描述】:

我正在开发一个 Web 服务,其中我拥有类型 A 的资源拥有类型 B 的资源。

我想开发一个 API 来获取关于 A 的统计信息,但只考虑其 B 的子集。

所以我有一条路线,采用 A 的 ID,B 的 ID 集合,并返回统计信息。我会像下面这样使用它:

POST /api/StatsAboutA/{aId}

JSON payload:
[1, 4, 12]

它会返回类似的东西:

[
  {"key": ..., "value": ...},
  ...
]

这是我的控制器:

class StatsAboutAController : ApiController
{
  // ...

  // POST api/StatsAboutA/{aId}
  [HttpPost]
  public Stats Post(long aId, IEnumerable<long> bIds)
  {
    A a = _aRepository.SelectById(aId);
    if (a == null)
    {
      throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
    }
    return _aRepository.CollectStats(a, bIds);
  }

  // ...

}

我无法配置我的路由器以使我的控制器匹配我的路由。

这是我收到的错误消息:

{
    "message": "No HTTP resource was found that matches the request URI 'http://localhost:51398/api/StatsAboutA/1'.",
    "messageDetail": "No action was found on the controller 'StatsAboutA' that matches the request."
}

【问题讨论】:

    标签: asp.net .net json


    【解决方案1】:

    将方法签名改为

    public Stats Post(long aId, [FromBody]IEnumerable<long> bIds)
    

    因为它试图从 URI 中获取 bIds 并且该路由未定义。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-01-13
      • 1970-01-01
      • 1970-01-01
      • 2015-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多