Send simple type from Angular to Web API in body of PUT request.

Angular, Web API, .NET, C#

We can use the HTTP PUT verb when updating a resource. In this example, I was activating a user's subscription by sending the subscription ID to a method on a Web API controller. We can use the put method on Angular's $http service, just make sure you inject it into your component first.

$http.put('/api/subscriptionapicontroller/activate', subscriptionId).
    success(function (response) {
        // success
    }).
    error(function (response) {
        // error
    });

This will send the subscriptionId parameter in the body of the request. As the subscriptionId parameter on the Activate method is a simple type, Web API will look for the value in the URL (which is not what we want), so in order to get Web API to look for the parameter in the body of the request, we decorate the parameter with the [FromBody] attribute.

[HttpPut]
public JsonResult Activate([FromBody]int subscriptionId)
{
    // code
}

Comments