wickjon
wickjon

Reputation: 920

POST with single parameter returns 404 error

I need to pass a single parameter to a Web API POST method.

Following is my AJAX call:

$http({ method: 'POST', url: "customers/ProcessCustomer/" + customerId })
    .success(function (data) {

    });

where customerId is a Guid .

And my controller:

[HttpPost]
[Route("customers/ProcessCustomer")]
public void ProcessCustomer(Guid id)
{
    //do some stuff
}

But I only get a 404 not found error when I do this. What am I doing wrong?

Upvotes: 0

Views: 191

Answers (2)

Tomas Aschan
Tomas Aschan

Reputation: 60664

When you POST to an action method, the data isn't embedded in the URL. Instead, use the data field of the settings object in your ajax call:

// I didn't recognize what library you're using for the AJAX call, so this is jQuery
$.ajax('customer/ProcessCustomer', {
    data: { id: customerId },
    success: function() { /* woohoo! */ }
});

Upvotes: 0

user247702
user247702

Reputation: 24222

You're using attribute routing but you haven't specified an id parameter in the route. Use this instead:

[Route("customers/ProcessCustomer/{id}")]

See Attribute Routing in Web API 2 for more examples.

Upvotes: 1

Related Questions