CRUD Web API Service Call Function In ASP.NET Default Web Application

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.          
  20.                 <asp:Button ID="Button2" runat="server" Text="Update" Width="124px" OnClick="Button2_Click" />    
  21.          
  22.                 <asp:Button ID="Button3" runat="server" Text="Select" Width="124px" OnClick="Button3_Click" />    
  23.          
  24.                 <asp:Button ID="Button4" runat="server" Text="Delete" Width="124px" OnClick="Button4_Click" />    
  25.             </p>    
  26.             <p>    
  27.                 <asp:GridView ID="GridView1" runat="server" BackColor="White" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" CellPadding="4" Width="658px">    
  28.                     <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />    
  29.                     <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />    
  30.                     <PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />    
  31.                     <RowStyle BackColor="White" ForeColor="#330099" />    
  32.                     <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />    
  33.                     <SortedAscendingCellStyle BackColor="#FEFCEB" />    
  34.                     <SortedAscendingHeaderStyle BackColor="#AF0101" />    
  35.                     <SortedDescendingCellStyle BackColor="#F6F0C0" />    
  36.                     <SortedDescendingHeaderStyle BackColor="#7E0000" />    
  37.                 </asp:GridView>    
  38.             </p>    
  39.             <p>    
  40.                  </p>           
  41.         <div class="col-md-4">    
  42.         </div>    
  43. </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.   
  6. namespace WebApiCall___APSX  
  7. {  
  8.     public class Users  
  9.     {  
  10.         public int ID { getset; }  
  11.         public string Username { getset; }  
  12.         public string Mobile { getset; }  
  13.     }  
  14. }  
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.   
  4.     HttpClient client = new HttpClient();  
  5.     client.BaseAddress = new Uri("http://localhost:2514/");  
  6.     client.DefaultRequestHeaders.Accept.Add(  
  7.         new MediaTypeWithQualityHeaderValue("application/json"));  
  8.   
  9.     HttpResponseMessage response = client.GetAsync("api/values").Result;  
  10.   
  11.     if (response.IsSuccessStatusCode)  
  12.     {  
  13.         var users = response.Content.ReadAsAsync<IEnumerable<Users>>().Result;  
  14.         GridView1.DataSource = users;  
  15.         GridView1.DataBind();  
  16.     }  
  17.     else  
  18.     {  
  19.          
  20.     }  
  21.   
  22. }   
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.   
  8.     var baseAddress = "http://localhost:2514/api/values/GetMobileInsert?Username=" + TextBox2.Text + "&Mobile=" + TextBox3.Text + "";  
  9.   
  10.     var http = (HttpWebRequest)WebRequest.Create(new Uri(baseAddress));  
  11.     http.Accept = "application/json";  
  12.     http.ContentType = "application/json";  
  13.     http.Method = "POST";  
  14.   
  15.     string parsedContent = json;  
  16.     ASCIIEncoding encoding = new ASCIIEncoding();  
  17.     Byte[] bytes = encoding.GetBytes(parsedContent);  
  18.   
  19.     Stream newStream = http.GetRequestStream();  
  20.     newStream.Write(bytes, 0, bytes.Length);  
  21.     newStream.Close();  
  22.   
  23.     var response = http.GetResponse();  
  24.   
  25.     var stream = response.GetResponseStream();  
  26.   
  27.     GetData();  
  28. }  
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.   
  4.     Users objProduct = new Users();  
  5.     objProduct.Username = TextBox2.Text;  
  6.     objProduct.Mobile = TextBox3.Text;  
  7.     string json = JsonConvert.SerializeObject(objProduct);  
  8.   
  9.     var baseAddress = "http://localhost:2514/api/values/GetMobileUpdate?ID=" + TextBox1.Text + "&Username=" + TextBox2.Text + "&Mobile=" + TextBox3.Text + "";  
  10.   
  11.     var http = (HttpWebRequest)WebRequest.Create(new Uri(baseAddress));  
  12.     http.Accept = "application/json";  
  13.     http.ContentType = "application/json";  
  14.     http.Method = "PUT";  
  15.   
  16.     string parsedContent = json;  
  17.     ASCIIEncoding encoding = new ASCIIEncoding();  
  18.     Byte[] bytes = encoding.GetBytes(parsedContent);  
  19.   
  20.     Stream newStream = http.GetRequestStream();  
  21.     newStream.Write(bytes, 0, bytes.Length);  
  22.     newStream.Close();  
  23.   
  24.     var response = http.GetResponse();  
  25.   
  26.     var stream = response.GetResponseStream();  
  27.   
  28.     GetData();  
  29. }  
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.   
  7.     var id = TextBox1.Text;  
  8.   
  9.     var url = "api/values/GetMobile?ID=" + id;  
  10.   
  11.     HttpResponseMessage response = client.GetAsync(url).Result;  
  12.   
  13.     if (response.IsSuccessStatusCode)  
  14.     {  
  15.         var result = response.Content.ReadAsStringAsync().Result;                               
  16.         JavaScriptSerializer serializer = new JavaScriptSerializer();  
  17.         List<Users>Us = serializer.Deserialize<List<Users>>(result);  
  18.         GridView1.DataSource = Us;  
  19.         GridView1.DataBind();  
  20.   
  21.     }  
  22.     else  
  23.     {  
  24.          
  25.     }  
  26. }  
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.   
  8.     var baseAddress = "http://localhost:2514/api/values?ID=" + TextBox1.Text + "";  
  9.   
  10.     var http = (HttpWebRequest)WebRequest.Create(new Uri(baseAddress));  
  11.     http.Accept = "application/json";  
  12.     http.ContentType = "application/json";  
  13.     http.Method = "DELETE";  
  14.   
  15.     string parsedContent = json;  
  16.     ASCIIEncoding encoding = new ASCIIEncoding();  
  17.     Byte[] bytes = encoding.GetBytes(parsedContent);  
  18.   
  19.     Stream newStream = http.GetRequestStream();  
  20.     newStream.Write(bytes, 0, bytes.Length);  
  21.     newStream.Close();  
  22.   
  23.     var response = http.GetResponse();  
  24.   
  25.     var stream = response.GetResponseStream();  
  26.   
  27.     GetData();  
  28. }    
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. 


Similar Articles