Web Services In ASP.Net Using C#

In this article I’ll demonstrate how to create simple Web Services In ASP.Net Using C#.

Introduction

A Web Service is a software program that uses XML to exchange information with other software via common internet protocols. In a simple sense, Web Services are a way for interacting with objects over the Internet.

 

Step 1: Create ASP.Net Web service source file.

Open VS2012, and create a new empty website and name it according to your ease. Add new file file web service.asmx.

WebService.asmx- which contains the following code:

<%@ WebService Language="C#" CodeBehind="~/App_Code/WebService.cs" Class="Service" %>

The page directive WebService is required and the class is the name of the .NET Class to expose the Web Service, each method exposes as Web Service Class Method need to have a declarative attribute statement.
 

Step 2: Code behind Web Service

WebService.cs

 


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 Service : System.Web.Services.WebService {

    public Service () {

        /*Uncomment the following line if using designed components 
        InitializeComponent(); */
    }
    /*you need to add [WebMethod] attribute for every function*/
    [WebMethod]        
    public int multiplication(int a, int b) {
        return (a * b);
    }

    [WebMethod]
    public int subtraction(int a, int b)
    {
        return (a - b);
    }
    [WebMethod]
    public int addition(int a, int b) {
        return (a + b);    
    }
    [WebMethod]
    public float division(float a, float b)
    {
        return (a / b);
    }
    
}

Here we need to focus on two main terms:

1. using System.Web.Services;

This directive allows you to refer to objects in the System.Web.Services namespace without having to fully qualify the request. This statement is optional, but if it is not included, every reference to an object in this namespace must be fully qualified. An example is the next line, which is our class declaration. With the using statement, it looks as follows in C#:

The [WebMethod] attribute

Step 3: Create Web Serivce Reference

Right click Solution Explorer and select “Add Web Reference”

step 1

 

 

 

 

 

 

 

 

Add Web Reference window will appear, click on “Advance” option

 

step 2

 

 

 

 

 

 

 

 

 

Service Reference setting window will appear, click on “Add Web Reference” tab

 

step 3

 

 

 

 

 

 

 

 

 

 

 

 

Add web reference widow will appear, here you have two options to add web service,

first you can add URL of the web service (if you have direct URL) and second is by browsing (in case you don’t have the direct URL). In this I’ve used the second option.

step 4

 

 

 

 

 

 

 

 

 

Once you’ve selected the web service, the URL of the web service will appear in the URL bar and you can see the web service. The last step is to click on Add Reference.

step 5

 

 

 

 

 

 

 

 

Now in solution explorer you can see some new files and folders, Web Service is ready to use.

step 6

 

 

 

 

 

 

 

 

 

 

 

 

Step 4: Using web service

In Default.aspx page you need to drag and drop 3 text box and 4 buttons.

 

Default.aspx

 


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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table>
        <tr>
            <th>Enter First Number</th>
            <td>
                <asp:TextBox ID="txt_first" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <th>
                Enter Second Number
            </th>
            <td>
                <asp:TextBox ID="txt_second" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <th>
                Result
            </th>
            <td>
                <asp:TextBox ID="txt_result" ReadOnly="true" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <th>
                <asp:Button ID="btn_multiply" runat="server" Text="Multiply" OnClick="btn_multiply_Click" />
            </th>
            <th>
                <asp:Button ID="btn_add" runat="server" Text="Add" OnClick="btn_add_Click"/>
            </th>
            <th>
                <asp:Button ID="btn_subtract" runat="server" Text="Subtract" OnClick="btn_subtract_Click"/>
            </th>
            <th>
                <asp:Button ID="btn_division" runat="server" Text="Division" OnClick="btn_division_Click"/>
            </th>
        </tr>
    </table>
    </div>
    </form>
</body>
</html>

Default.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
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btn_multiply_Click(object sender, EventArgs e)
    {
        localhost.Service ws = new localhost.Service();  //we need to create object of the web service

        int a = Convert.ToInt32(txt_first.Text);
        int b = Convert.ToInt32(txt_second.Text);
        int c = ws.multiplication(a, b);
        txt_result.Text = c.ToString();

    }
    protected void btn_add_Click(object sender, EventArgs e)
    {
        localhost.Service ws = new localhost.Service();  //we need to create object of the web service

        int a = Convert.ToInt32(txt_first.Text);
        int b = Convert.ToInt32(txt_second.Text);
        int c = ws.addition(a, b);
        txt_result.Text = c.ToString();
    }
    protected void btn_subtract_Click(object sender, EventArgs e)
    {
        localhost.Service ws = new localhost.Service();  //we need to create object of the web service

        int a = Convert.ToInt32(txt_first.Text);
        int b = Convert.ToInt32(txt_second.Text);
        int c = ws.subtraction(a, b);
        txt_result.Text = c.ToString();
    }
    protected void btn_division_Click(object sender, EventArgs e)
    {
        localhost.Service ws = new localhost.Service();  //we need to create object of the web service

        int a = Convert.ToInt32(txt_first.Text);
        int b = Convert.ToInt32(txt_second.Text);
        float a_f = (float)a;               // type casting, converting int to float
        float b_f = (float)b;
        float c = ws.division(a_f, b_f);
        txt_result.Text = c.ToString();
    }
}

Screen Shots

Have a look at the screen shot of this application:

step7

 

 

 

 

I’ll try with the division part.

 

step8

 

 

 

 

 

 

 

 

Result after invoking.

 

step 9

 

 

 

 

 

This same result can be obtained by executing in .aspx page.

 

step 10

 

 

 

 

 

 

 

Hope this article was useful. Keep visiting for new updates.


JavaScript, ASP.Net & PHP Web Developer. Connect with me on Facebook and Twitter.

Share This Post

Recent Articles

Powered by Paras Babbar · Designed by Paras Babbar