Reputation: 565
I am trying to make a PUT
call to my service to update a user
I get 404 when code hits my URL but in dev tools if I click URL it fails at it actually hits my controller. I'm thinking I'm passing my model wrong as parameter, can someone please point to me proper direction please, I'm not sure what I'm doing wrong.
here is my service call
var updateUser = function(user) {
return $http({
method: "PUT",
url: serviceBase + "/UserController/PutUser",
data: user
}).success(successCallBack).error(errorCallBack);
}
and here is my controller
[HttpPut]
[Route("api/UserController/PutUser")]
public IHttpActionResult PutUser(UserDto user)
{
try
{
return Ok();
}
catch (Exception)
{
return NotFound();
}
}
I tried using JSON.stringify(user)
in my $http
call as well and was out of luck get same 404
error but after clicking actual link it will hit my controller
Thank you for your advise!
Upvotes: 1
Views: 864
Reputation: 247153
While the common practice is to also include an identifier with PUT requests you can still achieve what you desire with some minor changes.
First update the URL for the intended endpoint in the controller.
NOTE: This is just an example. Modify to suit your specific needs.
public class UsersController : ApiController {
[HttpPut]
[Route("api/Users")]
public IHttpActionResult Put(User user) {
try {
//assuming some form of storage for models
repository.Users.Update(user);
return Ok();
} catch (Exception) {
return NotFound();
}
}
}
... and then update your service call...
var updateUser = function(user) {
return $http({
method: "PUT",
url: serviceBase + "/api/Users",
data: user
}).success(succesCallBAck).error(errorCallBack);
}
the user
will be sent in the body of the request to the controller.
Upvotes: 1
Reputation: 1216
You need to put the id of the user at the end of url. Your methods needs 2 parameters. One userId and one User.
You may also want to revise your url structure.
Upvotes: 0