Introduction
The API Web service call function in WPF application is highly secured. This is because no data is available on the local computer, so all are services oriented. The records insert and update, delete, select has been [HttpGet], [HttpPut], [HttpPost] and [HttpDelete] oriented functions.
Step 1
Create a new project.
Step 2
Choose Windows Desktop, and Select the WPF Forms App (.NET Framework)
Add Class Users.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WebAPIClient
{
public class Users
{
public int Id { get; set; }
public string Username { get; set; }
public string Mobile { get; set; }
}
}
Step 3
Design the WPF application.
Step 4 - [HttpGet], [HttpPut], [HttpPost] and [HttpDelete]
Declare the Headers and add on DLL Reference (System.Net.Http.Formatting, Newtonsoft.json)
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Windows;
Get all the data in Gridview List: [HttpGet]
private void GetData() {
HttpClient client = new HttpClient();
// client.BaseAddress = new Uri("http://localhost:56851/");
client.BaseAddress = new Uri("http://localhost:2514/");
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = client.GetAsync("api/values").Result;
if (response.IsSuccessStatusCode) {
var users = response.Content.ReadAsAsync < IEnumerable < Users >> ().Result;
usergrid.ItemsSource = users;
} else {
MessageBox.Show("Error Code" + response.StatusCode + " : Message - " + response.ReasonPhrase);
}
}
Call the Get data Function in MainWindow()
public MainWindow() {
InitializeComponent();
GetData();
}
Refer the below screenshot for the data grid view list:
Post the data in Gridview List: [HttpPost]
private void Button_Click_1(object sender, RoutedEventArgs e) {
Users objProduct = new Users();
objProduct.Username = txtUsername.Text;
objProduct.Mobile = txtMobile.Text;
string json = JsonConvert.SerializeObject(objProduct);
var baseAddress = "http://localhost:2514/api/values/GetMobileInsert?Username=" + txtUsername.Text + "&Mobile=" + txtMobile.Text + "";
var http = (HttpWebRequest) WebRequest.Create(new Uri(baseAddress));
http.Accept = "application/json";
http.ContentType = "application/json";
http.Method = "POST";
string parsedContent = json;
ASCIIEncoding encoding = new ASCIIEncoding();
Byte[] bytes = encoding.GetBytes(parsedContent);
Stream newStream = http.GetRequestStream();
newStream.Write(bytes, 0, bytes.Length);
newStream.Close();
var response = http.GetResponse();
var stream = response.GetResponseStream();
GetData();
}
Refer to the below screenshot for the data grid view Insert:
Put the data in Gridview List: [HttpPut]
private void BtnUpdate_Click(object sender, RoutedEventArgs e) {
Users objProduct = new Users();
objProduct.Username = txtUsername.Text;
objProduct.Mobile = txtMobile.Text;
string json = JsonConvert.SerializeObject(objProduct);
var baseAddress = "http://localhost:2514/api/values/GetMobileUpdate?ID=" + txtID.Text + "&Username=" + txtUsername.Text + "&Mobile=" + txtMobile.Text + "";
var http = (HttpWebRequest) WebRequest.Create(new Uri(baseAddress));
http.Accept = "application/json";
http.ContentType = "application/json";
http.Method = "PUT";
string parsedContent = json;
ASCIIEncoding encoding = new ASCIIEncoding();
Byte[] bytes = encoding.GetBytes(parsedContent);
Stream newStream = http.GetRequestStream();
newStream.Write(bytes, 0, bytes.Length);
newStream.Close();
var response = http.GetResponse();
var stream = response.GetResponseStream();
GetData();
}
Refer to the below screenshot for data grid view Update:
Get the Randomly selected value in data GridView:
private void btnSearch_Click(object sender, RoutedEventArgs e) {
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("http://localhost:2514/");
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
var id = txtID.Text;
var url = "api/values/GetMobile?ID=" + id;
HttpResponseMessage response = client.GetAsync(url).Result;
if (response.IsSuccessStatusCode) {
var result = response.Content.ReadAsStringAsync().Result;
var s = Newtonsoft.Json.JsonConvert.DeserializeObject(result);
MessageBox.Show(s.ToString());
} else {
MessageBox.Show("Fail");
}
}
Refer to the below screenshot for a randomly selected value in data GridView:
Refer to the below screenshot for a randomly selected value in data GridView:
private void btnDelete_Click(object sender, RoutedEventArgs e) {
Users objProduct = new Users();
objProduct.Username = txtUsername.Text;
objProduct.Mobile = txtMobile.Text;
string json = JsonConvert.SerializeObject(objProduct);
var baseAddress = "http://localhost:2514/api/values?ID=" + txtID.Text + "";
var http = (HttpWebRequest) WebRequest.Create(new Uri(baseAddress));
http.Accept = "application/json";
http.ContentType = "application/json";
http.Method = "DELETE";
string parsedContent = json;
ASCIIEncoding encoding = new ASCIIEncoding();
Byte[] bytes = encoding.GetBytes(parsedContent);
Stream newStream = http.GetRequestStream();
newStream.Write(bytes, 0, bytes.Length);
newStream.Close();
var response = http.GetResponse();
var stream = response.GetResponseStream();
GetData();
}
Refer to the below screenshot for data grid view Delete:
Summary
The web API call function is used in WPF applications and is one of the best options for high-level data security. I hope this method helps you to API Web service call in a WPF application.