asp.net web API HTTP PUT method

孤街醉人 提交于 2020-05-24 03:58:22

问题


I have some resource- UserProfile

public UserProfile
{
   public string Email{get;set;}
   public string Password{get;set;}
}

I want to change Email and Password separatly (only one for user at same time). I have web api controller for example /api/user/123 that handle requests in RESTful style. Follow the RESTful style i should have one method PUT which update the resource, but i have two task that update the same resource api/user/123. I need to add some feature to PUT request body like {email:'test@domain.com', changeType:'email'} or {password:'12345678',changeType:'password' } to write some if in my PUT method ? Or there is some other way to update my resource in RESTful style ?


回答1:


You have two options for updating email and password separately.

A) Don't use PUT, use POST

B) Create child resources for updating the individual elements, e.g.

PUT /api/user/123/email

And

PUT /api/user/123/password



回答2:


[HttpPut]
public HttpResponseMessage PutProduct(Product p)
{
    Product pro = _products.Find(pr => pr.Id == p.Id);

    if (pro == null)
        return new HttpResponseMessage(HttpStatusCode.NotFound);

    pro.Id = p.Id;
    pro.Name = p.Name;
    pro.Description = p.Description;

    return new HttpResponseMessage(HttpStatusCode.OK);
}


来源:https://stackoverflow.com/questions/21540279/asp-net-web-api-http-put-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!