Click here to Skip to main content
Click here to Skip to main content

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

, 4 Feb 2014
Rate this:
Please Sign up or sign in to vote.
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:

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"

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.

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.

License

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

Share

About the Author

Subodh Raghav, a software engineer 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 60 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 PinmemberGregory.Gadow5-Feb-14 5:49 
GeneralRe: My vote of 2 PinprofessionalJV99996-Feb-14 3:47 
SuggestionYour article does not present a clear idea, but it is not bad. PinprofessionalHamza I. Kanaan5-Feb-14 1:40 
SuggestionPretty much for beginner Level only. but a nice try. Pinmember VICK4-Feb-14 19:41 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140821.2 | Last Updated 4 Feb 2014
Article Copyright 2014 by Subodh Raghav
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid