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



在我的 Web API 中删除请求中,其中包含多个参数。我需要使用 C# 窗口表单应用程序和我的代码作为波纹管来使用此 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;

但这些都不起作用。如何将参数传递给删除请求。

更新:

这是我的控制器类,

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

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

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

相关内容

最新更新