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
- <%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApiCall___APSX._Default" %>
- <asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
- <div class="jumbotron" style="font-size: xx-large">
- Mobile Contact</div>
- <p>
- ID:
- <asp:TextBox ID="TextBox1" runat="server" Width="177px"></asp:TextBox>
- </p>
- <p>
- Username:
- <asp:TextBox ID="TextBox2" runat="server" Width="278px"></asp:TextBox>
- </p>
- <p>
- Mobile:
- <asp:TextBox ID="TextBox3" runat="server" Width="227px"></asp:TextBox>
- </p>
- <p>
- <asp:Button ID="Button1" runat="server" Text="Insert" Width="124px" OnClick="Button1_Click" />
-
- <asp:Button ID="Button2" runat="server" Text="Update" Width="124px" OnClick="Button2_Click" />
-
- <asp:Button ID="Button3" runat="server" Text="Select" Width="124px" OnClick="Button3_Click" />
-
- <asp:Button ID="Button4" runat="server" Text="Delete" Width="124px" OnClick="Button4_Click" />
- </p>
- <p>
- <asp:GridView ID="GridView1" runat="server" BackColor="White" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" CellPadding="4" Width="658px">
- <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
- <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
- <PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
- <RowStyle BackColor="White" ForeColor="#330099" />
- <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
- <SortedAscendingCellStyle BackColor="#FEFCEB" />
- <SortedAscendingHeaderStyle BackColor="#AF0101" />
- <SortedDescendingCellStyle BackColor="#F6F0C0" />
- <SortedDescendingHeaderStyle BackColor="#7E0000" />
- </asp:GridView>
- </p>
- <p>
- </p>
- <div class="col-md-4">
- </div>
- </asp:Content>
Screenshot in Design View
Add Class Users.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
-
- namespace WebApiCall___APSX
- {
- public class Users
- {
- public int ID { get; set; }
- public string Username { get; set; }
- public string Mobile { get; set; }
- }
- }
Step 4 - [HttpGet], [HttpPut], [HttpPost] and [HttpDelete]
Declare the Headers and add on dll Reference (System.Net.Http.Formatting, Newtonsoft.json)
- using System;
- using System.Collections.Generic;
- using System.Web.UI;
- using System.Net.Http;
- using System.Net.Http.Headers;
- using System.Net;
- using System.IO;
- using Newtonsoft.Json;
- using System.Text;
- using System.Web.Script.Serialization;
Get all the data in Gridview List: [HttpGet]
- private void GetData()
- {
-
- HttpClient client = new HttpClient();
- 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;
- GridView1.DataSource = users;
- GridView1.DataBind();
- }
- else
- {
-
- }
-
- }
Call the Get data Function in Page_Load ()
- protected void Page_Load(object sender, EventArgs e)
- {
- GetData();
- }
Refer to the below screenshot for the data grid view list:
Post the data in Gridview List: [HttpPost]
- protected void Button1_Click(object sender, EventArgs e)
- {
- Users objProduct = new Users();
- objProduct.Username = TextBox2.Text;
- objProduct.Mobile = TextBox3.Text;
- string json = JsonConvert.SerializeObject(objProduct);
-
- var baseAddress = "http://localhost:2514/api/values/GetMobileInsert?Username=" + TextBox2.Text + "&Mobile=" + TextBox3.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]
- protected void Button2_Click(object sender, EventArgs e)
- {
-
- Users objProduct = new Users();
- objProduct.Username = TextBox2.Text;
- objProduct.Mobile = TextBox3.Text;
- string json = JsonConvert.SerializeObject(objProduct);
-
- var baseAddress = "http://localhost:2514/api/values/GetMobileUpdate?ID=" + TextBox1.Text + "&Username=" + TextBox2.Text + "&Mobile=" + TextBox3.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 the data GridView:
- private void GetselectData()
- {
- HttpClient client = new HttpClient();
- client.BaseAddress = new Uri("http://localhost:2514/");
- client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
-
- var id = TextBox1.Text;
-
- var url = "api/values/GetMobile?ID=" + id;
-
- HttpResponseMessage response = client.GetAsync(url).Result;
-
- if (response.IsSuccessStatusCode)
- {
- var result = response.Content.ReadAsStringAsync().Result;
- JavaScriptSerializer serializer = new JavaScriptSerializer();
- List<Users>Us = serializer.Deserialize<List<Users>>(result);
- GridView1.DataSource = Us;
- GridView1.DataBind();
-
- }
- else
- {
-
- }
- }
Refer to the below screenshot for a randomly selected value in data GridView:
Delete the data in Gridview List: [HttpDelete]
- protected void Button4_Click(object sender, EventArgs e)
- {
- Users objProduct = new Users();
- objProduct.Username = TextBox2.Text;
- objProduct.Mobile = TextBox3.Text;
- string json = JsonConvert.SerializeObject(objProduct);
-
- var baseAddress = "http://localhost:2514/api/values?ID=" + TextBox1.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
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.