Reputation: 347
My question is how can I pass multiple parameter to DELETE request.
My controller class as follow,
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 as follow,
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);
}
}
}
I try to consume this using postman like this,http://localhost:10927/api/Task?id1=1&id2=5&id3="2018-03-14"
but which not working, please help me to solve this.
Upvotes: 3
Views: 20864
Reputation: 895
[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
Upvotes: -1
Reputation: 8256
Try passing a view model:
public class YourViewModel {
public int Id1 { get; set;}
public int Id2 { get; set;}
public string Id3 { get; set;}
}
Then
[HttpPost]
[Route("api/Task")]
public void Delete([FromBody] YourViewModel model)
{
TaskPersistent tp = new TaskPersistent();
tp.deleteTask(model.Id1, model.Id2, model.Id3);
}
In this way you don't have to specify the parameters in the query string. But you have to ensure that the request header has:
'Content-Type: application/json'
Update: In case you need to give it a try, this is how you need to call it from the client side in case you are using 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){ }
});
Upvotes: 3
Reputation: 441
Try the following
[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);
}
Call it via: http://localhost:10927/api/Task/1/2/"2018-03-14"
--- OR ---
[Route("api/Task")]
public void Delete(int id,int id2, string id3)
{
TaskPersistent tp = new TaskPersistent();
tp.deleteTask(id,id2,id3);
}
Call it via: http://localhost:10927/api/Task?id=1&id2=2&id3="2018-03-14"
Upvotes: 8