如何使用来自 windows 客户端应用程序的 DELETE 请求

How to consume DELETE request from windows client application

在我的网络 API 删除请求中,其中包含多个 parameters.I 需要使用 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 请求。

更新:

这是我的控制器class,

[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);
}

这是人class

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);
    }
}

试试下面的方法。它应该复制您尝试通过 PostMan 使用 API 的方式。

var response = client.DeleteAsync($"api/person/{p.ID}/{p.SID}/\"{p.Name}\"").Result;