在我的网页API删除请求中,其中包含多个参数。我需要使用C#windows窗体应用程序和我的代码来使用此DELETE请求。
private void btnDelete_Click(object sender, EventArgs e)
{
using (var client = new HttpClient())
{
person p = new person { ID = 1, SID = 5, Name = "paul"};
client.BaseAddress = new Uri("http://localhost:2733/");
var response = client.DeleteAsync("api/person/").Result;
if (response.IsSuccessStatusCode)
{
Console.Write("Success");
}
else
Console.Write("Error");
}
}
这是我使用Postman消费它的方式,它的工作正常 http://localhost:2733/api/person/1/5/"paul"
如何使用我的Windows客户端使用它。我试试这两种方式,
var response = client.DeleteAsync("api/person/",p).Result;
和
var response = client.DeleteAsync("api/person/"+1+5+"paul").Result;
但那些不起作用。如何将参数传递给DELETE请求。
更新
这是我的控制器类,
[Route("api/person/{id:int}/{pid:int}/{pname}")]
[HttpDelete]
public void Delete(int id, int pid, string pname)
{
var pModel = new PModel
{
ID = id,
SID = pid,
Name= pname
};
Person p = new Person();
p.deletePerson(pModel);
}
这是人类
public void deletePerson(PModel p)
{
try
{
string sql = $"DELETE from person WHERE ID = {p.ID} AND SID = {p.SID} AND Name= {p.Name}"; ;
MySql.Data.MySqlClient.MySqlCommand cmd = new MySql.Data.MySqlClient.MySqlCommand(sqlString, conn);
cmd.ExecuteNonQuery();
long x = cmd.LastInsertedId;
}
catch (MySqlException x)
{
int errr = x.Number;
Console.WriteLine(errr);
}
}
答案 0 :(得分:1)
尝试以下方法。它应该复制您尝试通过PostMan使用API的方式。
var response = client.DeleteAsync($"api/person/{p.ID}/{p.SID}/\"{p.Name}\"").Result;