[英]How to execute PUT method from angularJS to web api?
我真的不知道如何使用Web api的PUT方法從angular傳遞參數。
我知道GET方法是如何執行的,但是PUT,POST和DELETE對我來說真的很難。
我讀了很多文章,但我仍然不知道
我的網絡api中的控制器中有這樣的代碼:
static readonly IProfile profileRepository = new ProfileRepository();
[Route("api/profile/")]
[HttpGet]
[System.Web.Http.AcceptVerbs("GET")]
public IEnumerable<Profile> getProfiles()
{
return profileRepository.getProfiles();
}
[Route("api/profile/")]
[HttpPut]
[System.Web.Http.AcceptVerbs("PUT")]
public IEnumerable<Profile> putProfile(Profile profile)
{
profileRepository.putProfile(profile);
return getProfiles();
}
我的angularJS也有這樣的服務
var _putProfile = function (name,address,contacts) {
return $http.put(serviceURL + 'api/profile/Name=' + name + '&Address=' + address + '&Contact=' + contacts).then(function (results) {
return results;
});
};
當我使用Postman
應用程序時,當我使用x-www-form-urlencoded
時,Web api執行得很好,並且它將數據從Postman傳遞到Web api,但是如何將數據從angularJS傳遞到Web api。
有沒有人可以在這里給我最好的答案。.請給我一個想法,並告訴我什么是最佳實踐。
我只是angularJS和Web Api的新功能,請指導我。
AngularJS發送json數據,而不發送x-www-form-urlencoded
格式數據。 Web API具有讀取兩者的功能。 當涉及到HTTP PUT動詞時,數據應該在正文中傳遞而不是查詢字符串。
對於$ http.put調用,您應該執行以下操作。
$http.put(serviceURL + 'api/profile', { Name:name, Address:address, Contact:contacts});
請閱讀$ http文檔。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.