0

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

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.