简体   繁体   English

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

[英]How to execute PUT method from angularJS to web api?

I don't really have an idea how to use PUT method of web api passing the parameter from angular. 我真的不知道如何使用Web api的PUT方法从angular传递参数。

I understand how GET method executed but the PUT, POST and DELETE are really hard for me. 我知道GET方法是如何执行的,但是PUT,POST和DELETE对我来说真的很难。

I read many articles but it i still dont get an idea 我读了很多文章,但我仍然不知道

I have a code like this in controller in my web api: 我的网络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();
    }

I also have like this in service in my angularJS 我的angularJS也有这样的服务

 var _putProfile = function (name,address,contacts) {
        return $http.put(serviceURL + 'api/profile/Name=' + name + '&Address=' + address + '&Contact=' + contacts).then(function (results) {
            return results;
        });
    };

When i use Postman application the web api execute well when i use x-www-form-urlencoded and it passes data from postman to web api but how to pass data from angularJS to web api. 当我使用Postman应用程序时,当我使用x-www-form-urlencoded时,Web api执行得很好,并且它将数据从Postman传递到Web api,但是如何将数据从angularJS传递到Web api。

Is there anyone can give me a best answer here.. Please give me an idea how to do it and please advice me what is the best practice.. 有没有人可以在这里给我最好的答案。.请给我一个想法,并告诉我什么是最佳实践。

Im only new in angularJS and Web Api please guide me.. Thanks you so much 我只是angularJS和Web Api的新功能,请指导我。

AngularJS send json data and not x-www-form-urlencoded format data. AngularJS发送json数据,而不发送x-www-form-urlencoded格式数据。 Web API has capability of reading both. Web API具有读取两者的功能。 When it comes to HTTP PUT verb, data should be passed in body not query string. 当涉及到HTTP PUT动词时,数据应该在正文中传递而不是查询字符串。

For your $http.put call you should do something like this. 对于$ http.put调用,您应该执行以下操作。

$http.put(serviceURL + 'api/profile', { Name:name, Address:address, Contact:contacts});

Please read $http documentation. 请阅读$ http文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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