How to host an webpage as service?

Posted by in ASP.NET category on for Intermediate level | Views : 4907 red flag

Generally hosting .aspx page as services is not suggested as Web Service is especially built for this purpose. However, in some scenario this is likely to avoid an extra layer of SAOP because of Web Services protocol and to avoid adding reference to the consuming clients.
In this article, I am going to show how to host .aspx page ( web page) as service, receive a request as xml contents and respond accordingly. One can use this while sending request from BizTalk Server to a .aspx page and getting response (using HTTP Adapter) OR communicating with a legacy application from .net and sending the response back OR posting the form programmatically to a webpage and getting the response back.
How to develop an webpage page as services
Simply create a .aspx page with your desired name (I have named it as "aspxService.aspx"), remove the contents from .aspx page except the page directives and write the following code in code behind file (.aspx.cs). You need to use

using System.IO;
using System.Text;

namespace to work with Stream and StringBuilder object.

protected void Page_Load(object sender, EventArgs e)


// Create a Stream object to capture the InputStream.

using (Stream strm = Request.InputStream)


// Find number of bytes in stream.

int strmLen = (int)strm.Length;

// Create a byte array that will hold stream.

byte[] byteArr = new byte[strmLen];

// Read stream into byte arrays.

strm.Read(byteArr, 0, strmLen);

// Convert byte arrays to a text string.

string requestString = string.Empty;

StringBuilder strB = new StringBuilder();


for (int i = 0; i < strmLen; i++)




requestString = strB.ToString();

if (requestString.Length > 0)


// clear all contents, if any





// call your method and pass the requestString (this can be xml that will be understood by the Load method)

MyData data = GetMyDataBAL.Load(requestString);

// Response.Write("Input string is <hr />" + requestString);



catch (Exception ee)


lbl1.Text = "Error occured." + ee.Message;





Get solutions of the .NET problems with video explanations, .pdf and source code in .NET How to's.

In the above code, first I am getting all the Input into the Stream object using Request.InputStream (whatever will be posted on this page will be in the form of Request.InputStream), reading the stream into byte array and ultimately reading the array into the StringBuilder. In this way I have all the data that has been posted on this page in requestString variable.
Next, I am checking for the length of the requestString to validate if I have something has been really posted and have this into the requestString and calling my BAL method to get the desired data based on the requestString.
Please note that this will only work if the source form have FORM method as "post" (<form method="post" />), "get" method will not work here as "get" appends all the data of the form in the url as a querystring.
Sample form to post data

<form method="post" action="aspxService.aspx">

<input type="text" name="inputt" />

<input type="submit" value="Submit" />


Above is the sample form (it can be either in .html or .aspx) that can be used to test this functionality. If you enter "Sheo Narayan" into the texbox and click Submit button; you will get following result provided you have commented the MyData ... and uncomment the Response.Write line in the Page_Load code snippet above.

Input string is 
An web page can be hosted as a service that can receive a request and respond back accordingly.
Page copy protected against web site content infringement by Copyscape

About the Author

Full Name: Sheo Narayan
Member Level: HonoraryPlatinum
Member Status: Administrator
Member Since: 7/8/2008 6:32:14 PM
Country: India
Regards, Sheo Narayan
Ex-Microsoft MVP, Author, Writer, Mentor & architecting applications since year 2001. Connect me on | |

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)