繁体   English   中英

如何执行从angularJS到Web API的PUT方法?

[英]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.

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