Here is my API controller method

 [HttpPut]

        public List<Employee> PutEmployee(int EmpID, Employee empl)
        {
            empl.EmpID = EmpID;
            int index = emp.GetEmployees().FindIndex(i => i.EmpID == empl.EmpID);
            emp.GetEmployees().RemoveAt(index);
            emp.GetEmployees().Add(empl);
            return emp.GetEmployees();
        }

Here is angularJS controller

   $scope.UpdateEmp = function () {
        var empl = $scope.Employee;
        empHttpFactory.update(empl.EmpID,empl ).success(function () { // update method in my resource uses $resource
            $location.path('/EmpList'); //redirect to list
        });

    };

Here is my service code

 var resource={
        update: function (ID, empl) {

           // var empl = {'EmpID':1,'FirstName':'Kailash', 'Salary':62000};
            var params = { EmpID: ID, empval: empl };

                         // return $http.put('../api/Emp/Put', { EmpID: ID , empval:   return $http({
                method: 'PUT',
                url: '../api/Emp/Put',
                data: params

            })

empl });

        }
    }

    return resource;

When i do the debug, i could see the value for EMPID and empval in the angularjs controller and service but when it comes to API controller part, i m getting the value for EMPID alone but not for empval, its coming as null. can someone provide me working sample of it. i m new to AngularJS

Update 1 :

I have replaced the Params to data in my http call. My API method expecting the parameter in the body as per my controller method. I changed the http call accordingly and it started working fine. Also in my first argument to the controller method, I m using the variable name as EmpID, if I have used as "id", then probably, id would have been passed as the parameter in the URL itself

 return $http({
                    method: 'PUT',
                    url: '../api/Emp/Put',
                    data: params

                })

Updated Question: This is regarding the $resource

In $resource documentation, 2nd argument is parameter and the 3rd has list of actions where I can mention the parameters. But if you see from my example, I haven't explicitly used params option but passing data to my controller method. Its working as expected because its taking it as data. Why it so???

$resource('../../Employee/PutEmployee/', { }, { update: { method: 'PUT', empval: { "EmpID": 1, "FirstName": "John", "LastName": "Peter", "Salary": 60000 } ,isArray: true } })

share|improve this question
2  
What is the REST api supposed to receive. If you had to document this REST service, what would you say about its URL, the body of the request, etc.? – JB Nizet May 20 '15 at 17:13
    
its api/Emp/EmpID – TechQuery May 20 '15 at 17:17
    
@JB Nizet, I updated my question with the solution I used and also I have another question which I posted – TechQuery May 21 '15 at 14:08
    
@JBNizet, I updated my question with the solution I used and also I have another question which I posted – TechQuery May 21 '15 at 14:08

Here is an example of my toying web api project.

public class MoviesApiController: ApiController {
[HttpGet]
public IEnumerable<Movie> Get() { /* your implementation here */ }
[HttpGet]
public Movie Get(int Id) { /* your implementation here */ }
[HttpPut]
public HttpResponseMEssage Put(int Id) {/* your impl */ }
[HttpPost]
public HttpResponseMessage Post(Movie movie) {/* impl */}
[HttpDelete]
public HttpResponseMessage Delete(int id) { /*impl */ }

In fiddler, calll get with endpoint like: api/MoviesApi/

Call with specific movie id with endpoint like: api/MoviesApi/1

To issue a put, in fiddler change action to put, and rest endpoint like: api/MoviesApi/1

The same url goes for post and delete. Make sure you change it appropriately in fiddler

As for angularjs, I use $resource instead of $http, but your url should be the same. An example of my angularjs service:

angular.module("your_name").factory("your_name", ['$resource', function($resource) {
return $resource('/api/MoviesApi/:Id', null, {
'update' : {method: 'PUT'}});
share|improve this answer
    
But for update(Put), you need to pass the model as well rit? Atleast in my case, i m passing the model as well for update. So in this case, how i can decorate my API and how i call it in angular. – TechQuery May 20 '15 at 18:00
    
You can do like @JB Nizet's answer for using $http, I am just showing how you use the url because in your question, your backend put method's name is PutEmployee but in your angularjs, the url is api/Employee/Put and I think that's why you get the error "the request resource does not support http method put" – ngunha02 May 20 '15 at 18:11
    
But it says the message as but it says the message as “{"Message":"The requested resource does not support http method 'PUT'."} – TechQuery May 20 '15 at 18:20
    
In fiddler, did you change your verb to put? – ngunha02 May 20 '15 at 18:22
    
In don't have fiddler with me now, but if I remeber correctly, in fiddler in composer tab, on the left of the url input, there is a drop down list that let you change from get, post, delete,..bla bla. Also, as JB Nizet's comment, looks like you have problem with interpret webapi – ngunha02 May 20 '15 at 18:33

If I understand correctly, the REST service is bound to the URL api/Emp/:EmpID, with method PUT, and expects to receive the new employee data as JSON, in the request body.

But that's not what you're doing in your angular code. You're sending a PUT request on the URL ../api/Emp/Put?EmpID=<theID>&empval=<the employee>, without any request body.

What you need to do is

$http({
    method: 'PUT',
    url: '../api/Emp/' + ID,
    data: empl
});

or, simpler:

$http.put('../api/Emp/' + ID, empl);
share|improve this answer
    
I decorated my Put method like this [HttpPut] [Route("~/api/Emp/ID")] and i changed my service code as return $http({ method: 'PUT', url: '../api/Emp/' + ID, data: empVal }) but in fiddler it shows the message as {"Message":"The requested resource does not support http method 'PUT'."} – TechQuery May 20 '15 at 17:34
    
OK. It seems that your problem is more about how to implement your backend and interpret the annotations (which I assume are C# annotations), rather than an AngularJS problem. Please tag your question with the appropriate tags (.Net, C#, I guess). – JB Nizet May 20 '15 at 17:38
    
Are you doing RESTful, if so, I think you may need to change your api action from PutEmployee to Put then you use url like : '..api/Emp/:Id'. Or you can keep it the way it is which in your case use custom action name, and in your javascript, change url to something like ../api/PutEmployee/:ID – ngunha02 May 20 '15 at 17:39
    
@JackyNguyen can you please share me simple working sample with API controller method and how it needs to be called in $http as well – TechQuery May 20 '15 at 17:48
    
@JBNizet, hi, i have posted another query related to angularjs, could you help me with it stackoverflow.com/questions/33654437/angularjs-resource-cach‌​e – TechQuery Nov 12 '15 at 20:11

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.