0

I am facing "Http failure response" for put method, when i try to update my userdetails on userdetails page of my angular 2 application. Can any one provide me idea to fix the following error.

Error:- Message: "The requested resource does not support http method 'PUT'." message: "Http failure response for http://localhost/TestWebAPI/api/UserDetails: 405 Method Not Allowed"

userdetails.component.ts

onUpdateUserClick(index)
{   
    this.objuserservice.UpdateUser(this.UpdateUser).subscribe
    (
        (response) => {
            this.UpdateUser = response;
            this.userslist[this.updateIndex].email = this.UpdateUser.email;
            this.userslist[this.updateIndex].personname = this.UpdateUser.personname;
            this.userslist[this.updateIndex].mobile = this.UpdateUser.mobile;
            this.userslist[this.updateIndex].dateofbirth = this.UpdateUser.dateofbirth;
            this.userslist[this.updateIndex].monthofbirth = this.UpdateUser.monthofbirth;this.userslist[this.updateIndex].yearofbirth=this.UpdateUser.yearofbirth;
            this.userslist[this.updateIndex].gender = this.UpdateUser.gender;
            this.userslist[this.updateIndex].country = this.UpdateUser.country;
        },
        (error)=>{ 
        });  
}

users.service.ts

UpdateUser(userobj:User):Observable<User>
{
    return this.http.put<User>(`/TestWebAPI/api/UserDetails`,userobj,responseType:"json"});              
}

Here is my asp.netwebapi(TestWebAPI) project code,

public void Put(int id, [FromBody]user objuser)
{
    dbentity.Entry(objuser).State = System.Data.Entity.EntityState.Modified;
    dbentity.SaveChanges();
}
5
  • Maybe it's a problem that you are not passing that id? You could try to modify your method to be just public void Put([FromBody]user objuser) and put a breakpoint in the method just for testing, to see if that request would be correctly handled. Commented Nov 23, 2018 at 11:29
  • On second thought, if you have the default routes in Web API, maybe you should pass that id in the URL, from Angular, like this.http.put<User>("/TestWebAPI/api/UserDetails/" + userObj.Id, userobj, responseType:"json"});. Commented Nov 23, 2018 at 11:35
  • Thanks Dan , it works Commented Nov 24, 2018 at 4:35
  • @rajula - Great, I've made an answer out of that comment, so it would be more visible, you can accept it if you want. Commented Nov 24, 2018 at 9:44
  • yes, i accept it with happiness Commented Nov 25, 2018 at 12:01

1 Answer 1

1

If you have the default routes in Web API, you should pass that id in the URL from Angular, like:

UpdateUser(userobj:User):Observable<User>
{  
    this.http.put<User>("/TestWebAPI/api/UserDetails/" + userObj.Id, userobj,
        responseType:"json"});
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.