使用单个参数的POST返回404错误

时间:2014-10-08 14:57:27

标签: c# ajax post asp.net-web-api

我需要将一个参数传递给Web API POST方法。

以下是我的AJAX电话:

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

    });

其中customerIdGuid

我的控制员:

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

但是当我这样做时,我只收到 404 not found 错误。我做错了什么?

2 个答案:

答案 0 :(得分:1)

您正在使用属性路由,但您尚未在路线中指定id参数。请改用:

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

有关更多示例,请参阅Attribute Routing in Web API 2

答案 1 :(得分:0)

当您POST操作方法时,数据不会嵌入到网址中。相反,请在ajax调用中使用设置对象的data字段:

// 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! */ }
});