简体   繁体   中英

asp.net web API HTTP PUT method

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 ?

[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);
}

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, eg

PUT /api/user/123/email

And

PUT /api/user/123/password

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM