我正在使用web api 2(用mvc 5)来从MongoDB商店访问一些数据。这是动作/方法:

    [Route("api/container/children/{parentId}")]
    [HttpGet]
    public HttpResponseMessage GetChildren(String parentId) {
        ObjectId containerId;
        if (!ObjectId.TryParse(parentId, out containerId)) {
            IEnumerable<Container> containers = this.Connection.GetCollection<Container>().FindAs<Container>(Query.EQ(Container.FieldNames.ParentId, containerId));
            return this.Request.CreateResponse<Container[]>(HttpStatusCode.OK, containers.ToArray());
        }

        return this.Request.CreateResponse(HttpStatusCode.NotFound);
    }
.

从jQuery调用jQuery以$ .get致电参数objectId.empty(这是0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000)给我一个404:

/API/容器/儿童/000000000000000000000000

但调用此URL工作正常:

/ api / container / shinken / 0000000000000000

Web API 2上的(ID)参数的长度是否有一些限制?

有帮助吗?

解决方案

这不是一个webapi问题。

代码中的错误实际上非常简单。在IF条件

中删除NOT(!)运算符
[Route("api/container/children/{parentId}")]
[HttpGet]
public HttpResponseMessage GetChildren(String parentId) {
    ObjectId containerId;
    if (ObjectId.TryParse(parentId, out containerId)) {
        ...
    }

    return this.Request.CreateResponse(HttpStatusCode.NotFound);
}
.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top