Http-сообщение об ошибке для метода put в angular 2 с помощью asp.net webapi - PullRequest
0 голосов
/ 23 ноября 2018

Я сталкиваюсь с "Http fail response" для метода put, когда я пытаюсь обновить свои пользовательские детали на странице userdetails моего приложения angular 2.Может ли кто-нибудь дать мне идею исправить следующую ошибку.

Ошибка: - Сообщение: «Запрашиваемый ресурс не поддерживает http-метод« PUT ».»сообщение: «Http-сообщение об ошибке для http://localhost/TestWebAPI/api/UserDetails: 405 метод не разрешен»

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

Вот мой код проекта asp.netwebapi (TestWebAPI),

public void Put(int id, [FromBody]user objuser)
{
    dbentity.Entry(objuser).State = System.Data.Entity.EntityState.Modified;
    dbentity.SaveChanges();
}

1 Ответ

0 голосов
/ 24 ноября 2018

Если у вас есть маршруты по умолчанию в Web API, вы должны передать это id в URL из Angular, например:

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