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 and 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

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

/// <summary>
/// Summary description for WebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService
{
    public WebService()
    {
        // Uncomment the following line if using designed components
        // InitializeComponent();
    }

    [WebMethod]
    public string HelloWorld()
    {
        return "Hello World";
    }

    [WebMethod]
    public int Mul(int a, int b)
    {
        return a * b;
    }

    [WebMethod]
    public int Add(int a, int b)
    {
        return a + b;
    }

    [WebMethod]
    public int Div(int a, int b)
    {
        return a / b;
    }
}

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

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .style1 {
            width: 301px;
        }
        .style3 {
            width: 245px;
            font-weight: bold;
        }
        .style4 {
            width: 245px;
        }
        .style5 {
            width: 301px;
            font-weight: bold;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <table style="width:100%;">
                <tr>
                    <td class="style3">
                        <strong>Enter Your First Number:</strong>
                    </td>
                    <td class="style1">
                        <strong>
                            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                        </strong>
                    </td>
                    <td>
                        <strong>Your Answer:</strong>
                    </td>
                </tr>
                <tr>
                    <td class="style3">
                        Enter Your Second Number:
                    </td>
                    <td class="style1">
                        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                    </td>
                    <td>
                        <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td class="style3">
                    </td>
                    <td class="style5">
                        What do you want to do ??
                    </td>
                    <td>
                    </td>
                </tr>
                <tr>
                    <td class="style4">
                    </td>
                    <td class="style1">
                        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Addition" />
                        <asp:Button ID="Button2" runat="server" onclick="Button2_Click" Text="Multiply" />
                        <asp:Button ID="Button3" runat="server" Height="25px" onclick="Button3_Click" Text="Division" />
                    </td>
                    <td>
                    </td>
                </tr>
                <tr>
                    <td class="style4">
                    </td>
                    <td class="style1">
                    </td>
                    <td>
                    </td>
                </tr>
            </table>
        </div>
    </form>
</body>
</html>

Your design looks like the 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

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    WebService webserv = new WebService();

    protected void Page_Load(object sender, EventArgs e)
    {
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        int add;

        add = webserv.add(int.Parse(TextBox1.Text), int.Parse(TextBox2.Text));
        TextBox3.Text = add.ToString();
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        int mul;

        mul = webserv.mul(int.Parse(TextBox1.Text), int.Parse(TextBox2.Text));
        TextBox3.Text = mul.ToString();
    }

    protected void Button3_Click(object sender, EventArgs e)
    {
        int div;

        div = webserv.div(int.Parse(TextBox1.Text), int.Parse(TextBox2.Text));
        TextBox3.Text = div.ToString();
    }
}

Output chamber

Output chamber

Addition

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


Similar Articles