简体   繁体   中英

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.

I understand how GET method executed but the PUT, POST and DELETE are really hard for me.

I read many articles but it i still dont get an idea

I have a code like this in controller in my web 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

 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.

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 send json data and not x-www-form-urlencoded format data. Web API has capability of reading both. When it comes to HTTP PUT verb, data should be passed in body not query string.

For your $http.put call you should do something like this.

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

Please read $http documentation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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