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:’[email protected]’, 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 ?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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

Method 2

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x