Rest 为什么Web API方法会忽略实际值并假设第一条记录=#1?

Rest 为什么Web API方法会忽略实际值并假设第一条记录=#1?,rest,controller,asp.net-web-api,repository,uri,Rest,Controller,Asp.net Web Api,Repository,Uri,WebAPI项目的存储库类中的以下代码: public DepartmentRepository() { Add(new Department { Id = 0, AccountId = "7.0", DeptName = "Dept7" }); Add(new Department { Id = 1, AccountId = "8.0", DeptName = "Dept8" }); Add(new Department { Id = 2, AccountId = "9

WebAPI项目的存储库类中的以下代码:

public DepartmentRepository()
{
    Add(new Department { Id = 0, AccountId = "7.0", DeptName = "Dept7" });
    Add(new Department { Id = 1, AccountId = "8.0", DeptName = "Dept8" });
    Add(new Department { Id = 2, AccountId = "9.0", DeptName = "Dept9" });
}
…由控制器类中的此代码调用:

public Department GetDepartment(int id)
{
    Department dept = repository.Get(id);
    if (dept == null)
    {
        throw new HttpResponseException(HttpStatusCode.NotFound);
    }
    return dept;
}
…在浏览器中使用此选项:

http://localhost:48614/api/departments/1/
…返回以下内容:

<Department xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/DuckbillServerWebAPI.Models">
<AccountId>7.0</AccountId>
<DeptName>Dept7</DeptName>
<Id>1</Id>
</Department>
更新2 在回答Mike Wasson的问题时,以下是Add方法:

public Department Add(Department item)
{
    if (item == null)
    {
        throw new ArgumentNullException("item");
    }
    item.Id = _nextId++;
    departments.Add(item);
    return item;
}
我添加/发布项目的代码(同样,基于Mike Wasson文章的代码)是:

公共HttpResponseMessage邮局(部门)
{
dept=repository.Add(dept);
var response=Request.CreateResponse(HttpStatusCode.Created,dept);
字符串uri=Url.Link(“DefaultApi”,新的{id=dept.id});
response.Headers.Location=新Uri(Uri);
返回响应;
}

注意:从注释线程中,repository类改编自本文:


Add方法分配ID,覆盖您发布的任何值。那篇文章中的repository类实际上只是为了演示Web API。但在典型的应用程序中,ID可能是一个主DB密钥,客户端不会在POST中指定ID。但这取决于应用程序。

注意:从注释线程中,repository类改编自本文:


Add方法分配ID,覆盖您发布的任何值。那篇文章中的repository类实际上只是为了演示Web API。但在典型的应用程序中,ID可能是一个主DB密钥,客户端不会在POST中指定ID。但这取决于应用程序。

您是否检查了
GetDepartment
调用中
id
的值?另外,存储库的
Get()
方法中的代码是什么样的?Add()方法是什么样的?在您的示例响应中,ID为1,因此看起来Add方法可能正在覆盖ID。(您从哪里获得这个存储库类?)我还没有添加Add()方法(将直接添加);如果我理解了你的第二个问题,我根据本文中的一个(事实上是你的)建模了这个存储库(没有双关语)@米凯瓦森:不要介意前面评论的第一部分;我已经用这个更新了我的帖子。啊-这是我的怀疑:-)Add方法分配了ID,覆盖了你发布的任何值。事实上,那篇文章中的repository类不是很好,它只是为了演示WebAPI。(我可能应该更改它…)但在典型的应用程序中,ID可能是主DB密钥,客户端不会在POST中指定ID。但这取决于应用程序。您是否检查了
GetDepartment
调用中
id
的值?另外,存储库的
Get()
方法中的代码是什么样的?Add()方法是什么样的?在您的示例响应中,ID为1,因此看起来Add方法可能正在覆盖ID。(您从哪里获得这个存储库类?)我还没有添加Add()方法(将直接添加);如果我理解了你的第二个问题,我根据本文中的一个(事实上是你的)建模了这个存储库(没有双关语)@米凯瓦森:不要介意前面评论的第一部分;我已经用这个更新了我的帖子。啊-这是我的怀疑:-)Add方法分配了ID,覆盖了你发布的任何值。事实上,那篇文章中的repository类不是很好,它只是为了演示WebAPI。(我可能应该更改它…)但在典型的应用程序中,ID可能是主DB密钥,客户端不会在POST中指定ID。但这取决于应用程序。
public Department Add(Department item)
{
    if (item == null)
    {
        throw new ArgumentNullException("item");
    }
    item.Id = _nextId++;
    departments.Add(item);
    return item;
}
public HttpResponseMessage PostDepartment(Department dept)
{
    dept = repository.Add(dept);
    var response = Request.CreateResponse<Department>(HttpStatusCode.Created, dept);

    string uri = Url.Link("DefaultApi", new { id = dept.Id });
    response.Headers.Location = new Uri(uri);
    return response;
}