Introduction
The goal of this article is to explore .NET web services. Web services are standardized way of integrating Web-based applications. The program is developed with Microsoft
Visual Studio 2010 using C# as the programming language.
Task: Creating a simple Text Editor web application using multiple web services.
Background
This article is written to make you more familiar with the functions of .NET and the usage of multiple Web Services in a single web application. Before attempting to read this article,
you should have a basic knowledge of C# as the programming language and use of Microsoft Visual Studio 2010 to create web services. Please note that
.NET Framework 4 is used in the sample code. You also need to have Windows 7 installed on your computer.
Task: A simple Text Editor Web application
Create a simple Text Editor Web application using Web Form with the following functions (Please note that you have to create a web service for each of these functions):
- Word Count: counts all the words in the main text.
- Word Occurrence Count: counts the number of a specific word occurrence in the main text.
- Replace Text: replaces the two specified words in the main text.
You should have a drop down list containing the required operations that upon selection of each operation, the relevant text boxes should appear (Please note that you are required
to display the identified messages in each case):
- For the Word Count, only the main text box should appear
- For the Word Occurrence Count, another text box in addition to the main text box should appear
- For the Replace Text, two other text boxes in addition to the main text box should appear
You are also required to check all the validations:
- None of the text boxes should be empty when the relevant operation happens
- When the Replace Text happens, if the word that has to be replaced did not exist, an error message should be displayed (you can use the Word Count web service for this purpose).
Using the code
Create a Website named as Editor in VS2010.
Add 3 web services named as
textReplace.asmx, WordCounter.asmx, and WordOccurenceCounter.asmx in the website.
The code below is written in textReplace.asmx web service to replace the words in the text editor.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
namespace Editor
{
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
public class textReplace : System.Web.Services.WebService
{
[WebMethod]
public string ReplaceWord(string Data, string Match, string Replace)
{
int count = 0;
int i = 0;
while ((i = Data.ToLower().IndexOf(Match, i)) != -1)
{
i += Match.Length;
count++;
}
Data = Data.ToLower().Replace(Match, Replace);
return count + "_" + Data;
}
}
}
The following code below in WordCounter.asmx is written to count the words in the text editor.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
namespace Editor
{
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
public class WordCounter : System.Web.Services.WebService
{
[WebMethod]
public int WordCount(string Data)
{
string[] DataArray = Data.Split(' ');
int length = 0;
for (int i = 0; i < DataArray.Length; i++)
{
if (!(DataArray[i].Equals("")))
length++;
}
return length;
}
}
}
The code below is written in WordOccurenceCounter
to find the occurrence of a word in a text written in the text editor.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
namespace Editor
{
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
public class WordOccurenceCounter : System.Web.Services.WebService
{
[WebMethod]
public int WordOccurence(string Data, string Match)
{
int count = 0;
int i = 0;
while ((i = Data.ToLower().IndexOf(Match, i)) != -1)
{
i += Match.Length;
count++;
}
return count;
}
}
}
After creating the 3 web services, compile the project.
To use these web services in the current project, you need to add their reference in the project.
For this right click the project in Solution Explorer, add Service reference. Then add the reference of the above services.
In the Default.aspx page, I have created a method ReplaceOccurance()
in which I am using the reference of web service 'textReplace
' .
The is shown below:
private void ReplaceOccurence()
{
lblSearchWord.Visible = true; lblReplaceWord.Visible = true;
txtSearchWord.Visible = true; txtReplaceWord.Visible = true;
textReplace trep = new textReplace();
string ReplaceText = trep.ReplaceWord(txtEdit.Text, txtSearchWord.Text, txtReplaceWord.Text);
string NoOfOccurence = ReplaceText.Substring(0, ReplaceText.IndexOf("_"));
if (NoOfOccurence.Equals("0"))
{
lblReplaceWordCount.Text = txtSearchWord.Text + " not found in the text.";
}
else
{
ReplaceText = ReplaceText.Substring(ReplaceText.IndexOf("_") + 1,
ReplaceText.Length - (ReplaceText.IndexOf("_") + 1));
if (NoOfOccurence.Equals("1"))
lblReplaceWordCount.Text = NoOfOccurence + " word replaced";
else
lblReplaceWordCount.Text = NoOfOccurence + " words replaced";
txtEdit.Text = ReplaceText;
}
lblSearchWordCount.Visible = false; lblSearchWordCount.Text = string.Empty;
lblSearchWord.Style.Add("display", "block");
txtSearchWord.Style.Add("display", "block");
lblReplaceWord.Style.Add("display", "block");
txtReplaceWord.Style.Add("display", "block");
}
Similarly I have created 2 other methods namely CountOccurence()
and CountWords()
which are using the reference of web services
WordOccurenceCounter
and WordCounter
.
Methods are shown below:
private void CountOccurence()
{
WordOccurenceCounter woc = new WordOccurenceCounter();
int Count = woc.WordOccurence(txtEdit.Text, txtSearchWord.Text);
if (Count == 0)
{
lblSearchWordCount.Text = txtSearchWord.Text + " not found in the text.";
lblSearchWordCount.Visible = true;
lblSearchWordCount.ForeColor = System.Drawing.Color.Red;
}
else if (Count > 0)
{
lblSearchWordCount.Text = Convert.ToString(Count) + " matches found";
lblSearchWordCount.Visible = true;
lblSearchWordCount.ForeColor = System.Drawing.Color.Black;
}
lblSearchWord.Style.Add("display", "block");
txtSearchWord.Style.Add("display", "block");
}
private void CountWords()
{
WordCounter wc = new WordCounter();
int Count = wc.WordCount(txtEdit.Text);
lblWordCount.Visible = true;
lblWordCount.Text = "Total Word in text: " + Count.ToString();
}
Run the program to see the efficient working of web services.
Points of Interest
This article gives you a basic knowledge of creating web services in a web application.