C# 为什么DELETE中发送的Guid总是默认值?
我在Web Api中有一个C# 为什么DELETE中发送的Guid总是默认值?,c#,jquery,.net,ajax,asp.net-core-webapi,C#,Jquery,.net,Ajax,Asp.net Core Webapi,我在Web Api中有一个HttpDelete方法: [HttpDelete] public async Task<IActionResult> Delete(Guid id) <== {00000000-0000-0000-0000-000000000000} { //some code return Ok("{\"msg\":\"success\"}"); } 我正在使用fnRowCallback初始化一个数据表,以调用deletePatient如下: "
HttpDelete
方法:
[HttpDelete]
public async Task<IActionResult> Delete(Guid id) <== {00000000-0000-0000-0000-000000000000}
{
//some code
return Ok("{\"msg\":\"success\"}");
}
我正在使用fnRowCallback
初始化一个数据表,以调用deletePatient
如下:
"fnRowCallback": function (nRow, aData, iDisplayIndex, iDisplayIndexFull) {
var idPatient = aData[4];
if (idPatient != null) {
$('td:eq(4)', nRow).html('<img id="' + idPatient + '" onclick="deletePatient("' + idPatient + '");" src="../images/delete.png" />');
}
}
“fnRowCallback”:函数(nRow、aData、iDisplayIndex、iDisplayIndexFull){
var idPatient=aData[4];
如果(idPatient!=null){
$('td:eq(4)”,nRow.html(“”);
}
}
当我调试在Delete
方法中收到的Guid时,我只获得Guid的默认值:{00000000-0000-0000-0000-000000000000}
我的错在哪里,有人能帮我吗
谢谢
编辑:
对于postman,它工作得很好(raw、JSON(application/JSON)):
url:api/Patient/d4fee000-aa6a-4673-81cc-33e990509d73
确切的网址:
好的,所以我稍微修改了一下我的通话:
function deletePatient(idPatient) {
var url = "api/Patient/" + idPatient;
$.ajax({
url: url,
method: 'DELETE',
dataType: 'json',
success: function (response) {
alert("Ok !");
},
error: function (response) {
alert("Delete : Une erreur est survenue, merci de retenter plus tard.");
}
});
}
和我的http delete方法以正确捕获Id:
[HttpDelete("{id}")]
public async Task<IActionResult> Delete(Guid id)
{...}
[HttpDelete(“{id}”)]
公共异步任务删除(Guid id)
{...}
感谢大家的帮助,它现在运行得非常好。您的数据不需要
{id:idPatient}
吗(因为它似乎是一个查询参数)?@FrancoisBorgies要么向我们展示idPatient
的样子,要么尝试数据:{id:idPatient}
除了这个问题之外,删除异步:false
。这是非常糟糕的做法。如果您需要更具体的帮助,请告诉我们您是如何检索idPatient
值的,因为这是问题的相关部分。您可以显示发布对象的结构idPatient
看起来您只需要在Ajax中发布{“id”:idPatient}
,为什么需要传递`数据:{id:idPatient},`它已经在urlone点中为您;-)
[HttpDelete("{id}")]
public async Task<IActionResult> Delete(Guid id)
{...}