将多个参数传递给 DELETE 请求 c# web API
Passing multiple parameter to DELETE request c# web API
我的问题是如何将多个参数传递给 DELETE 请求。
我的控制器class如下,
namespace MYAPI1.Controllers
{
public class TaskController : ApiController
{
// DELETE: api/Task/5
[Route("api/Task/id1/id2/{id3}")]
public void Delete(int id,int id2, string id3)
{
TaskPersistent tp = new TaskPersistent();
tp.deleteTask(id,id2,id3);
}
}
}
TaskPersistent.class如下,
public class TaskPersistent
{
public void deleteTask(int id, int id2, string id3)
{
try
{
string sqlString = "DELETE from devproj WHERE (DeveloperID, ProjectID, WorkDate) = VALUES ('" + id + "', '" + id2 + "', '" + id3 + "');";
MySql.Data.MySqlClient.MySqlCommand cmd = new MySql.Data.MySqlClient.MySqlCommand(sqlString, conn);
cmd.ExecuteNonQuery();
long x = cmd.LastInsertedId;
}
catch (Exception x)
{
Console.WriteLine(x);
}
}
}
我尝试像这样使用邮递员来使用它,http://localhost:10927/api/Task?id1=1&id2=5&id3="2018-03-14"
但是这不起作用,请帮我解决这个问题。
尝试以下方法
[Route("api/Task/{id:int}/{id2:int}/{id3}")]
public void Delete(int id,int id2, string id3)
{
TaskPersistent tp = new TaskPersistent();
tp.deleteTask(id,id2,id3);
}
通过以下方式调用:http://localhost:10927/api/Task/1/2/"2018-03-14"
--- 或 ---
[Route("api/Task")]
public void Delete(int id,int id2, string id3)
{
TaskPersistent tp = new TaskPersistent();
tp.deleteTask(id,id2,id3);
}
通过以下方式调用:http://localhost:10927/api/Task?id=1&id2=2&id3="2018-03-14"
尝试传递视图模型:
public class YourViewModel {
public int Id1 { get; set;}
public int Id2 { get; set;}
public string Id3 { get; set;}
}
然后
[HttpPost]
[Route("api/Task")]
public void Delete([FromBody] YourViewModel model)
{
TaskPersistent tp = new TaskPersistent();
tp.deleteTask(model.Id1, model.Id2, model.Id3);
}
这样您就不必在查询字符串中指定参数。但是你必须确保请求头有:
'Content-Type: application/json'
更新:
如果您需要尝试一下,这就是您在使用 JQuery:
时需要从客户端调用它的方式
var myModel= { Id1:1, Id2:11 Id3:"test" }
$.ajax({
type: 'POST',
url: 'http://localhost:10927/api/Task',
data: JSON.stringify(myModel),
contentType: 'application/json;',
dataType: 'json',
success: function(data){ }
});
[HttpDelete]
public async Task<IActionResult> Delete(List<string> ids)
{
await _mapService.RemoveAsync(ids);
var ret = CreatedAtAction(nameof(Delete), new { ids = ids }, ids);
return ret;
}
Curl
curl -X 'DELETE' \
'https://localhost:44307/api/Map' \
-H 'accept: */*' \
-H 'Content-Type: application/json' \
-d '[
"623b35de9f6cedc3a22f7b37",
"623b35de9f6cedc3a22f7b38"
]'
Response body
Download
[
"623b35de9f6cedc3a22f7b37",
"623b35de9f6cedc3a22f7b38"
]
Response headers
content-type: application/json; charset=utf-8
date: Wed,23 Mar 2022 15:00:39 GMT
location: https://localhost:44307/api/Map?ids=623b35de9f6cedc3a22f7b37&ids=623b35de9f6cedc3a22f7b38
server: Microsoft-IIS/10.0
x-powered-by: ASP.NET
我的问题是如何将多个参数传递给 DELETE 请求。
我的控制器class如下,
namespace MYAPI1.Controllers
{
public class TaskController : ApiController
{
// DELETE: api/Task/5
[Route("api/Task/id1/id2/{id3}")]
public void Delete(int id,int id2, string id3)
{
TaskPersistent tp = new TaskPersistent();
tp.deleteTask(id,id2,id3);
}
}
}
TaskPersistent.class如下,
public class TaskPersistent
{
public void deleteTask(int id, int id2, string id3)
{
try
{
string sqlString = "DELETE from devproj WHERE (DeveloperID, ProjectID, WorkDate) = VALUES ('" + id + "', '" + id2 + "', '" + id3 + "');";
MySql.Data.MySqlClient.MySqlCommand cmd = new MySql.Data.MySqlClient.MySqlCommand(sqlString, conn);
cmd.ExecuteNonQuery();
long x = cmd.LastInsertedId;
}
catch (Exception x)
{
Console.WriteLine(x);
}
}
}
我尝试像这样使用邮递员来使用它,http://localhost:10927/api/Task?id1=1&id2=5&id3="2018-03-14"
但是这不起作用,请帮我解决这个问题。
尝试以下方法
[Route("api/Task/{id:int}/{id2:int}/{id3}")]
public void Delete(int id,int id2, string id3)
{
TaskPersistent tp = new TaskPersistent();
tp.deleteTask(id,id2,id3);
}
通过以下方式调用:http://localhost:10927/api/Task/1/2/"2018-03-14"
--- 或 ---
[Route("api/Task")]
public void Delete(int id,int id2, string id3)
{
TaskPersistent tp = new TaskPersistent();
tp.deleteTask(id,id2,id3);
}
通过以下方式调用:http://localhost:10927/api/Task?id=1&id2=2&id3="2018-03-14"
尝试传递视图模型:
public class YourViewModel {
public int Id1 { get; set;}
public int Id2 { get; set;}
public string Id3 { get; set;}
}
然后
[HttpPost]
[Route("api/Task")]
public void Delete([FromBody] YourViewModel model)
{
TaskPersistent tp = new TaskPersistent();
tp.deleteTask(model.Id1, model.Id2, model.Id3);
}
这样您就不必在查询字符串中指定参数。但是你必须确保请求头有:
'Content-Type: application/json'
更新: 如果您需要尝试一下,这就是您在使用 JQuery:
时需要从客户端调用它的方式var myModel= { Id1:1, Id2:11 Id3:"test" }
$.ajax({
type: 'POST',
url: 'http://localhost:10927/api/Task',
data: JSON.stringify(myModel),
contentType: 'application/json;',
dataType: 'json',
success: function(data){ }
});
[HttpDelete]
public async Task<IActionResult> Delete(List<string> ids)
{
await _mapService.RemoveAsync(ids);
var ret = CreatedAtAction(nameof(Delete), new { ids = ids }, ids);
return ret;
}
Curl
curl -X 'DELETE' \
'https://localhost:44307/api/Map' \
-H 'accept: */*' \
-H 'Content-Type: application/json' \
-d '[
"623b35de9f6cedc3a22f7b37",
"623b35de9f6cedc3a22f7b38"
]'
Response body
Download
[
"623b35de9f6cedc3a22f7b37",
"623b35de9f6cedc3a22f7b38"
]
Response headers
content-type: application/json; charset=utf-8
date: Wed,23 Mar 2022 15:00:39 GMT
location: https://localhost:44307/api/Map?ids=623b35de9f6cedc3a22f7b37&ids=623b35de9f6cedc3a22f7b38
server: Microsoft-IIS/10.0
x-powered-by: ASP.NET