-2

In my web API delete request, which containing multiple parameters.I need to consume this DELETE request using C# windows form application and my code as bellow.

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

This is how I consume this using Postman, and its works finehttp://localhost:2733/api/person/1/5/"paul" How to consume this using my windows client. I try these two way,

var response = client.DeleteAsync("api/person/",p).Result;

and

var response = client.DeleteAsync("api/person/"+1+5+"paul").Result;

But those are not working. How can I pass parameters to DELETE request.

Updated:

This is my controller 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);
}

This is Person 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);
    }
}
Rooter
  • 383
  • 1
  • 7
  • 25

1 Answers1

1

Try the below. It should replicate the way you have tried consuming your API through PostMan.

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

Vidmantas Blazevicius
  • 4,652
  • 2
  • 11
  • 30
  • Sir this code print **Success** and no any errors display in application, but records are not delete – Rooter Mar 14 '18 at 12:35
  • @Rooter I mean, you have not even posted your controller code so as far as we know, it is an empty function that will return `Success` always as long as the route is correct (which it is since you've got the response back). Try and edit your question or create a new question showing your controller code becomes it could be (and most likely will) a completely separate problem not related to your initial question. – Vidmantas Blazevicius Mar 14 '18 at 12:39
  • @Rooter No problems, are you getting any exceptions in the Api side then? Can you tell us what the value of sql variable is? – Vidmantas Blazevicius Mar 14 '18 at 13:15
  • No any errors in API sir. How can I see the value of sql variable sir? – Rooter Mar 14 '18 at 13:25
  • Sir I put a break line in this line `string sql = $"DELETE from person WHERE ID = {p.ID} AND SID = {p.SID} AND Name= {p.Name}";` when i consume API using windows form, `p.name = "paul"` but when I consume it using Postman, `p.name = "\"paul\" "` – Rooter Mar 14 '18 at 13:46
  • @Rooter Ah ok, so you'ive edited my answer by changing `{p.Name}` to `\"{p.Name}\"` - a bit unusual to have the values like that but what can you do. This should work now for you. – Vidmantas Blazevicius Mar 14 '18 at 13:55
  • Thank you sir,now its works fine. Glad to find person like you :) what was the reason values come like that – Rooter Mar 14 '18 at 14:40