Introduction

The API Web service call function in Asp.net Default Web application is highly secure since there is no need for a business layer and Datalayer. All of them are service-oriented. The record insert and update, delete, select are [HttpGet], [HttpPut], [HttpPost] and [HttpDelete] oriented functions.
Step 1
Create a new project.
Step 2
Choose Web and select the ASP.NET Web application (.NET Framework)
Step 3
Design the ASP.NET Web Application.
Default.aspx
  1. <%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApiCall___APSX._Default" %>
  2. <asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
  3. <div class="jumbotron" style="font-size: xx-large">
  4. Mobile Contact</div>
  5. <p>
  6. ID:
  7. <asp:TextBox ID="TextBox1" runat="server" Width="177px"></asp:TextBox>
  8. </p>
  9. <p>
  10. Username:
  11. <asp:TextBox ID="TextBox2" runat="server" Width="278px"></asp:TextBox>
  12. </p>
  13. <p>
  14. Mobile:
  15. <asp:TextBox ID="TextBox3" runat="server" Width="227px"></asp:TextBox>
  16. </p>
  17. <p>
  18. <asp:Button ID="Button1" runat="server" Text="Insert" Width="124px" OnClick="Button1_Click" />
  19. <asp:Button ID="Button2" runat="server" Text="Update" Width="124px" OnClick="Button2_Click" />
  20. <asp:Button ID="Button3" runat="server" Text="Select" Width="124px" OnClick="Button3_Click" />
  21. <asp:Button ID="Button4" runat="server" Text="Delete" Width="124px" OnClick="Button4_Click" />
  22. </p>
  23. <p>
  24. <asp:GridView ID="GridView1" runat="server" BackColor="White" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" CellPadding="4" Width="658px">
  25. <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
  26. <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
  27. <PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
  28. <RowStyle BackColor="White" ForeColor="#330099" />
  29. <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
  30. <SortedAscendingCellStyle BackColor="#FEFCEB" />
  31. <SortedAscendingHeaderStyle BackColor="#AF0101" />
  32. <SortedDescendingCellStyle BackColor="#F6F0C0" />
  33. <SortedDescendingHeaderStyle BackColor="#7E0000" />
  34. </asp:GridView>
  35. </p>
  36. <p>
  37. </p>
  38. <div class="col-md-4">
  39. </div>
  40. </asp:Content>
Screenshot in Design View
Add Class Users.cs
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. namespace WebApiCall___APSX
  6. {
  7. public class Users
  8. {
  9. public int ID { get; set; }
  10. public string Username { get; set; }
  11. public string Mobile { get; set; }
  12. }
  13. }
Step 4 - [HttpGet], [HttpPut], [HttpPost] and [HttpDelete]
Declare the Headers and add on dll Reference (System.Net.Http.Formatting, Newtonsoft.json)
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Web.UI;
  4. using System.Net.Http;
  5. using System.Net.Http.Headers;
  6. using System.Net;
  7. using System.IO;
  8. using Newtonsoft.Json;
  9. using System.Text;
  10. using System.Web.Script.Serialization;
Get all the data in Gridview List: [HttpGet]
  1. private void GetData()
  2. {
  3. HttpClient client = new HttpClient();
  4. client.BaseAddress = new Uri("http://localhost:2514/");
  5. client.DefaultRequestHeaders.Accept.Add(
  6. new MediaTypeWithQualityHeaderValue("application/json"));
  7. HttpResponseMessage response = client.GetAsync("api/values").Result;
  8. if (response.IsSuccessStatusCode)
  9. {
  10. var users = response.Content.ReadAsAsync<IEnumerable<Users>>().Result;
  11. GridView1.DataSource = users;
  12. GridView1.DataBind();
  13. }
  14. else
  15. {
  16. }
  17. }
Call the Get data Function in Page_Load ()
  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3. GetData();
  4. }
Refer to the below screenshot for the data grid view list:
Post the data in Gridview List: [HttpPost]
  1. protected void Button1_Click(object sender, EventArgs e)
  2. {
  3. Users objProduct = new Users();
  4. objProduct.Username = TextBox2.Text;
  5. objProduct.Mobile = TextBox3.Text;
  6. string json = JsonConvert.SerializeObject(objProduct);
  7. var baseAddress = "http://localhost:2514/api/values/GetMobileInsert?Username=" + TextBox2.Text + "&Mobile=" + TextBox3.Text + "";
  8. var http = (HttpWebRequest)WebRequest.Create(new Uri(baseAddress));
  9. http.Accept = "application/json";
  10. http.ContentType = "application/json";
  11. http.Method = "POST";
  12. string parsedContent = json;
  13. ASCIIEncoding encoding = new ASCIIEncoding();
  14. Byte[] bytes = encoding.GetBytes(parsedContent);
  15. Stream newStream = http.GetRequestStream();
  16. newStream.Write(bytes, 0, bytes.Length);
  17. newStream.Close();
  18. var response = http.GetResponse();
  19. var stream = response.GetResponseStream();
  20. GetData();
  21. }
Refer to the below screenshot for the data grid view Insert:
Put the data in Gridview List: [HttpPut]
  1. protected void Button2_Click(object sender, EventArgs e)
  2. {
  3. Users objProduct = new Users();
  4. objProduct.Username = TextBox2.Text;
  5. objProduct.Mobile = TextBox3.Text;
  6. string json = JsonConvert.SerializeObject(objProduct);
  7. var baseAddress = "http://localhost:2514/api/values/GetMobileUpdate?ID=" + TextBox1.Text + "&Username=" + TextBox2.Text + "&Mobile=" + TextBox3.Text + "";
  8. var http = (HttpWebRequest)WebRequest.Create(new Uri(baseAddress));
  9. http.Accept = "application/json";
  10. http.ContentType = "application/json";
  11. http.Method = "PUT";
  12. string parsedContent = json;
  13. ASCIIEncoding encoding = new ASCIIEncoding();
  14. Byte[] bytes = encoding.GetBytes(parsedContent);
  15. Stream newStream = http.GetRequestStream();
  16. newStream.Write(bytes, 0, bytes.Length);
  17. newStream.Close();
  18. var response = http.GetResponse();
  19. var stream = response.GetResponseStream();
  20. GetData();
  21. }
Refer to the below screenshot for data grid view Update:
Get the Randomly selected value in the data GridView:
  1. private void GetselectData()
  2. {
  3. HttpClient client = new HttpClient();
  4. client.BaseAddress = new Uri("http://localhost:2514/");
  5. client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
  6. var id = TextBox1.Text;
  7. var url = "api/values/GetMobile?ID=" + id;
  8. HttpResponseMessage response = client.GetAsync(url).Result;
  9. if (response.IsSuccessStatusCode)
  10. {
  11. var result = response.Content.ReadAsStringAsync().Result;
  12. JavaScriptSerializer serializer = new JavaScriptSerializer();
  13. List<Users>Us = serializer.Deserialize<List<Users>>(result);
  14. GridView1.DataSource = Us;
  15. GridView1.DataBind();
  16. }
  17. else
  18. {
  19. }
  20. }
Refer to the below screenshot for a randomly selected value in data GridView:
Delete the data in Gridview List: [HttpDelete]
  1. protected void Button4_Click(object sender, EventArgs e)
  2. {
  3. Users objProduct = new Users();
  4. objProduct.Username = TextBox2.Text;
  5. objProduct.Mobile = TextBox3.Text;
  6. string json = JsonConvert.SerializeObject(objProduct);
  7. var baseAddress = "http://localhost:2514/api/values?ID=" + TextBox1.Text + "";
  8. var http = (HttpWebRequest)WebRequest.Create(new Uri(baseAddress));
  9. http.Accept = "application/json";
  10. http.ContentType = "application/json";
  11. http.Method = "DELETE";
  12. string parsedContent = json;
  13. ASCIIEncoding encoding = new ASCIIEncoding();
  14. Byte[] bytes = encoding.GetBytes(parsedContent);
  15. Stream newStream = http.GetRequestStream();
  16. newStream.Write(bytes, 0, bytes.Length);
  17. newStream.Close();
  18. var response = http.GetResponse();
  19. var stream = response.GetResponseStream();
  20. GetData();
  21. }
Refer to the below screenshot for data grid view Delete:

Summary

This web API call function is used in a ASP.NET default web application and is one of the best options for high-level data security. I hope this method helps you to API Web service call in an ASP.NET default web application.