Create A Simple Web Service In ASP.NET Using C#

We will make a simple mathematical operation for Addition, Multiplication and Division. A web service will be created.

Initial chamber

Step 1: Open Visual Studio 2010 and create an empty website. Give a suitable name webservice_demo.

Step 2: In Solution Explorer you get your empty website, add a web form, Web Service. Here are the steps:

For Web Form

webservice_demo (Your Empty Website) - Right Click, Add New Item, then Web Form. Name it webservice_demo.aspx.

For WebService
 

webservice_demo (Your Empty Website) - Right Click, Add New Item, then Web Service.

Web Service

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Services;  
  6.   
  7. /// <summary>  
  8. /// Summary description for WebService  
  9. /// </summary>  
  10. [WebService(Namespace = "http://tempuri.org/")]  
  11. [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]  
  12. // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.   
  13. // [System.Web.Script.Services.ScriptService]  
  14. public class WebService : System.Web.Services.WebService {  
  15.   
  16.     public WebService () {  
  17.   
  18.         //Uncomment the following line if using designed components   
  19.         //InitializeComponent();   
  20.     }  
  21.   
  22.     [WebMethod]  
  23.     public string HelloWorld() {  
  24.         return "Hello World";  
  25.     }  
  26.   
  27.   
  28.     [WebMethod]  
  29.     public int mul(int a, int b)  
  30.     {  
  31.   
  32.         return a * b;  
  33.       
  34.     }  
  35.   
  36.     [WebMethod]  
  37.     public int add(int a, int b)  
  38.     {  
  39.   
  40.         return a + b;  
  41.   
  42.     }  
  43.   
  44.     [WebMethod]  
  45.     public int div(int a, int b)  
  46.     {  
  47.         return a / b;  
  48.       
  49.     }  
  50.   
  51. }  
Design chamber

Step 3: Now open your webservice _demo.aspx file, where we create our simple design with the Textbox and a button.

webservice_demo.aspx
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>  
  2.   
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  4.   
  5. <html xmlns="http://www.w3.org/1999/xhtml">  
  6. <head runat="server">  
  7.     <title></title>  
  8.     <style type="text/css">  
  9.         .style1  
  10.         {  
  11.             width: 301px;  
  12.         }  
  13.         .style3  
  14.         {  
  15.             width: 245px;  
  16.             font-weight: bold;  
  17.         }  
  18.         .style4  
  19.         {  
  20.             width: 245px;  
  21.         }  
  22.         .style5  
  23.         {  
  24.             width: 301px;  
  25.             font-weight: bold;  
  26.         }  
  27.     </style>  
  28. </head>  
  29. <body>  
  30.     <form id="form1" runat="server">  
  31.     <div>  
  32.       
  33.         <table style="width:100%;">  
  34.             <tr>  
  35.                 <td class="style3">  
  36.                     <strong>Enter Your First Number:</strong></td>  
  37.                 <td class="style1">  
  38.                     <strong>  
  39.                     <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>  
  40.                     </strong>  
  41.                 </td>  
  42.                 <td>  
  43.                     <strong>Your Answer:</strong></td>  
  44.             </tr>  
  45.             <tr>  
  46.                 <td class="style3">  
  47.                     Enter Your Second Number:</td>  
  48.                 <td class="style1">  
  49.                     <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>  
  50.                 </td>  
  51.                 <td>  
  52.                     <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>  
  53.                 </td>  
  54.             </tr>  
  55.             <tr>  
  56.                 <td class="style3">  
  57.                      </td>  
  58.                 <td class="style5">  
  59.                     What do you want to do ??</td>  
  60.                 <td>  
  61.                      </td>  
  62.             </tr>  
  63.             <tr>  
  64.                 <td class="style4">  
  65.                      </td>  
  66.                 <td class="style1">  
  67.                     <asp:Button ID="Button1" runat="server" onclick="Button1_Click"   
  68.                         Text="Addition" />  
  69.                     <asp:Button ID="Button2" runat="server" onclick="Button2_Click"   
  70.                         Text="Multiply" />  
  71.                     <asp:Button ID="Button3" runat="server" Height="25px" onclick="Button3_Click"   
  72.                         Text="Division" />  
  73.                 </td>  
  74.                 <td>  
  75.                      </td>  
  76.             </tr>  
  77.             <tr>  
  78.                 <td class="style4">  
  79.                      </td>  
  80.                 <td class="style1">  
  81.                      </td>  
  82.                 <td>  
  83.                      </td>  
  84.             </tr>  
  85.         </table>  
  86.       
  87.     </div>  
  88.     </form>  
  89. </body>  
  90. </html>  
Your design looks like following image:

design

Code chamber

Step 4: Open your webservice_demo.aspx.cs and write some code so that our application starts working.

webservice_demo.aspx.cs
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7.   
  8. public partial class _Default : System.Web.UI.Page  
  9. {  
  10.     WebService webserv = new WebService();  
  11.     protected void Page_Load(object sender, EventArgs e)  
  12.     {  
  13.   
  14.     }  
  15.     protected void Button1_Click(object sender, EventArgs e)  
  16.     {  
  17.        
  18.         int add;  
  19.         
  20.   
  21.         add = webserv.add(int.Parse(TextBox1.Text), int.Parse(TextBox2.Text));  
  22.         TextBox3.Text = add.ToString();  
  23.   
  24.   
  25.     }  
  26.     protected void Button2_Click(object sender, EventArgs e)  
  27.     {  
  28.         int mul;  
  29.   
  30.       mul= webserv.mul(int.Parse(TextBox1.Text), int.Parse(TextBox2.Text));  
  31.         TextBox3.Text = mul.ToString();  
  32.   
  33.   
  34.     }  
  35.     protected void Button3_Click(object sender, EventArgs e)  
  36.     {  
  37.         int div;  
  38.   
  39.         div = webserv.div(int.Parse(TextBox1.Text), int.Parse(TextBox2.Text));  
  40.        TextBox3.Text = div.ToString();  
  41.   
  42.     }  
  43. }  
Output chamber

Output

addition

Hope you liked it. Thank you for reading. Have a good day.

 

Up Next
    Ebook Download
    View all
    Learn
    View all