(C# ASP.NET Core) Call a PUT and DELETE Web API From a C# WPF Application

As you must have already seen in the previous tutorials, we have a WPF application that obtains a list of records from our ASPNET Core web api application, and shows them in a WPF datagrid. When a row of the datagrid is clicked, a dialog pops-up that shows a form for editing the record. The save button for this form is wired to a click event handler that makes a PUT call to a web api and updates the record. A delete button on the same form causes a call to a DELETE web api.
(Rev. 31-Oct-2024)

Categories | About |     |  

Parveen,

Objective of this tutorial

The objective of this tutorial is to study the code for calling PUT and DELETE web api.

Open the solution explorer of the attached WPF project and locate the file DlgDoctorXaml.cs. Double click and open this file.

First we have the namespaces, then we can scroll down and locate the event handler for the update code.

As usual, we have used the HttpClient class for our web api communication.

Then, we have created a DoctorDTO on the basis of the form filled by the user. Validation code has not been shown for simplicity. PutAsJsonAsync function makes a call to the put web api.

The response message is shown in a messagebox.

// click handler of the update button 
// see the linked video for details 
private async void btnUpdateDoctor_Click(object sender, RoutedEventArgs e)
{

  String message = "Success";

  // make an API request 
  using (HttpClient client = new HttpClient())

  {

    // from the textboxes 
    // validation code not added for simplicity 
    var doctor = new DoctorDTO()
    {

      Fees = Convert.ToInt32(txtFees.Text),
      Name = txtDoctorName.Text
    };

    try
    {

      // this is the actual function 
      // that causes the call to the 
      // PUT Web Api 
      HttpResponseMessage response =
           await client.PutAsJsonAsync(
              $"{MainWindow.SvcIP}/{Doctor.Id}", doctor);

      response.EnsureSuccessStatusCode();

    }

    catch(Exception ex)
    {

      message = ex.Message;

    }

  }

  MessageBox.Show(this, message, "Message", MessageBoxButton.OK);

  DialogResult = true;

}

Next, let us also have a look at the event handler for the delete button.

Again, we have used the HttpClient class. The web api call is made by the DeleteAsync function.

// click handler of the delete button 
// see the linked video for details 
private async void btnDeleteDoctor_Click(object sender, RoutedEventArgs e)
{

  string message = string.Empty;

  // make an API request 
  using (HttpClient client = new HttpClient())

  {

    try
    {

      // this is the actual function 
      // that causes the call to the 
      // DELETE Web Api 
      HttpResponseMessage response =
          await client.DeleteAsync($"{MainWindow.SvcIP}/{Doctor.Id}");

      response.EnsureSuccessStatusCode();

      message = await response.Content.ReadAsStringAsync();

    }

    catch (Exception ex)
    {

      message = ex.Message;

    }

  }

  MessageBox.Show(this, message, "Message", MessageBoxButton.OK);

  DialogResult = true;

}

Video Explanation (see it happen!)

Please watch the following youtube video:

Run the Application

Finally, we can run the application.

First run the web api project to open the home page. This ensures that the server is running.

Now run the WPF project. If everything goes fine, the records will appear in the datagrid. Double-click a row to open the edit dialog form. Make some changes - change the name of the doctor, and change the value of fees. These changes will now be posted to the server. Click the submit button - we verify that the server response is received in a message box. Click OK to close the messagebox. We can see that the datagrid now shows the updated values.

Similarly, we can double click a row to again open the edit dialog box. Click on the delete button. We verify that the record gets removed from the datagrid.

Thus, we have successfully tested the PUT and DELETE web api. Thanks!


This Blog Post/Article "(C# ASP.NET Core) Call a PUT and DELETE Web API From a C# WPF Application" by Parveen is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.