Click here to Skip to main content
15,887,596 members
Articles / Web Development / ASP.NET
Tip/Trick

[You can do it] How to Create a Web service in ASP.NET

Rate me:
Please Sign up or sign in to vote.
1.81/5 (11 votes)
21 Apr 2016CPOL2 min read 14.2K   11   4
A web service is a web application (a piece of software component) which is available on the internet.

Introduction

In this article, you will learn what is a web service in ASP.NET, how it communicates with the other web applications and how to create a web service in ASP.NET.

What is a Web Service

A web service is a web application (a piece of software component) which is available on the internet. A web service uses XML format for all the communication with other applications on the internet. It uses XML and JSON data formats and HTTP protocol to communicate with other applications over the internet.

For example, let's say I have created a web application that can convert a string into uppercase. If I want it to be used by other applications on the internet, then it has to be converted into a web service and should be deployed on the net.

In the next few lines, you will see how to create a web service in ASP.NET and how to use it:

Step 1: Create a web service file and name it "HelloWorld.asmx" in Visual Studio. The extension for a web service file is .asmx:

Image 1

Step 2: After you have created the web service file, a code file "HelloWorld.vb" will be created in App_Code folder.

Step 3: Open this code file and write your custom code for the web service functionality. This web service will return a "Hello World" when it is consumed"

VB.NET
1.Imports System.Web
2.Imports System.Web.Services
3.Imports System.Web.Services.Protocols
4. 
5.' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
6.' <System.Web.Script.Services.ScriptService()> _
7.<WebService(Namespace:="http://tempuri.org/")> _
8.<WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
9.<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
10.Public Class HelloWorld
11.     Inherits System.Web.Services.WebService
12. 
13.    <WebMethod()> _
14.    Public Function HelloWorld() As String
15.        Return "Hello World"
16.    End Function
17. 
18.End Class 

Step 4: Okay. The service is created but how to use it. To use this service, create a simple .aspx page and paste the following code in it. In the action part, put the location of the web service. Here I have used my local host URL. Replace action part with your web service URL.

ASP.NET
1.<%@ Page Language="VB" AutoEventWireup="false" 
CodeFile="HelloWorld.aspx.vb" Inherits="HelloWorld" %>
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.</head>
9.<body>
10.    <form id="form1" runat="server" 
action="http://localhost:49572/wsDemo/HelloWorld.asmx/HelloWorld"  method="POST">
11.        <input type="submit" value="Run Web Service"> </input>
12.    </form>
13.</body>
14.</html>

Step 5: Here is the final output "Hello World" in the form of XML. You can read this XML data to use in your program.

 

Image 2

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
India India
Subodh Raghav, a Software Architect and certified web enthusiast, acquired his formal education and training at Aligarh Muslim University, India. He holds a Masters Degree in Computer Application and as of the present time; takes pride in working in the I.T. industry.

Subodh’s first article had been featured and published in EzineArticles.com and ArticlesBase.com among many others. As a programming enthusiast and a self-confessed “programming addict”, he has created various software applications to maximize the lifestyles of the modern internet users and other people who share his passion for computer technology. With over 1K+ original posts in his blog, he continues to impart his expertise to others by way of posting new and valuable information that can make a major difference in people’s lives.

Subodh’s hobbies and personal interests include writing informative blogs and surfing the internet; and he also takes inspiration from music and physical sports such as Cricket. Currently, he works for a IT Company in Noida, India; and is the author of his own blog.

Comments and Discussions

 
GeneralMy vote of 2 Pin
Gregory Gadow5-Feb-14 5:49
Gregory Gadow5-Feb-14 5:49 
GeneralRe: My vote of 2 Pin
JV99996-Feb-14 3:47
professionalJV99996-Feb-14 3:47 
Plus lately it's mostly all about WCF instead of the regular web services Smile | :) . I haven't googled lately (quite a lot of years ago) on how to make a web service, but I can imagine that most articles nowadays are about WCF. So there might actually be added value for this article Wink | ;) .
SuggestionYour article does not present a clear idea, but it is not bad. Pin
Hamza I. Kanaan5-Feb-14 1:40
professionalHamza I. Kanaan5-Feb-14 1:40 
SuggestionPretty much for beginner Level only. but a nice try. Pin
VICK4-Feb-14 19:41
professional VICK4-Feb-14 19:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.