Building .NET Application for Windows Apache Server


Summary

In this article I would like to discuss writing .NET applications which run on Windows Apache Server and IIS, issues concerned and deploying those application on both the servers.

Introduction

Software Requirements

Operating System : Windows 98, Windows NT, Windows ME, and Windows2000
Database : Any database (Preferably SQL Server 7.0/2000)
Apache Server : Latest Apache Server for Windows (
http://www.apache.org)
Others : .NET Framework Beta 2 SDK (
http://msdn.microsoft.com/downloads/default.asp)

Configuration

At times you are required to configure Apache to your needs. In those situations go to conf directory which could be found, at the following location X:\Program Files\Apache Group\Apache where X is the drive name where you installed the apache server.
Open the file named httpd.conf and modify your ServerName or user, etc to suit your needs.

Deployment

Apache Server

Place all .exe files into cgi-bin directory, which could be found, at the following location X:\Program Files\Apache Group\Apache, where X is the drive name where you installed the apache server.

Place all html documents into htdocs directory, which could be found, at the following location X:\Program Files\Apache Group\Apache, where X is the drive name where you installed the apache server.

Internet Information Server

Place all .exe files into scripts directory, which could be found, at the following location X:\Inetpub, where X is the drive name where you installed the IIS.

Place the entire html files into wwwroot directory or a sub-directory of your choice, under wwwroot.

Writing apache applications is very simple. Let us take a look at the following program. It is a just like a normal C# program that out puts Hello how are u but for the statement Console.WriteLine(Content-Type:text/html\n). This statement makes a big difference. This statement tells the program that the output must be thrown to a browser rather than a console.

Compile the hello.cs program in the normal way. It will compile without any errors. Try running the program from command prompt, to your surprise you will find the output to be as follows. I am compiling all the programs from the D:\Apache.

D:\Apache>csc Hello.cs
Microsoft (R) Visual C# Compiler Version 7.00.9254 [CLR version v1.0.2914]
Copyright (C) Microsoft Corp 2000-2001. All rights reserved.

D:\Apache>hello

Out put

Cotent-Type:text/html

Hello how are u

As can be seen from above, the output we got contains an extra statement Hello How are U. Its because, console can never understand what that statement is all about. Hence it printed it like a normal output statement.

Now try to place the hello.exe either in cgi-bin directory of Apache or Scripts directory of IIS. Open Web Browser and enter either http://localhost/cgi-bin/Hello.exe or http://localhost/scripts/hello.exe. You will now find the output as Hello how are u. It works as expected, the reason being our program throws the output in the language that browser understands. So, it is very important that we use the Console.WriteLine(Cotent-Type:text/html\n") whenever we wish to direct the output to a browser.

Usually, this is not the way we call web pages. Ideally, one would like to call a web page by typing http://localhost/hello.html In order to achieve that place Hello.html page in htdocs directory on apache or wwwroot directory on IIS. In the Hello.html page, write a form, whose action attribute value is either /cgi-bin/hello.exe or /scripts/hello.exe depending on the web server you are using. Open Web Browser and enter either http://localhost/Hello.html or http://localhost/Hello.html. Click Show Message button and you will find the output as Hello how are u.

To summarize, we can say that files with .exe extension execute at the server and are returned in different file formats, not necessarily html. The word Content-Type alerts the browser of incoming message type that it can format in the appropriate manner, should the browser understand the Content-Type mentioned by you. The content-type we define some times is also called as header type, which is usually, is separated by : symbol in the Console.WriteLine(Content-Type:text/html\n) statement. Again \n in Console.WriteLine is a must because this is how different headers are separated in a program. Having understood the basic concepts of how to install, configure, deploy and write .NET applications that run on Apache and IIS we shall move to some finer examples but the principles remain the same in all the programs.

Example 1: Hello program

//csc Hello.cs
using System;
class Hello
{
public static void Main()
{
Console.WriteLine("Cotent-Type:text/html\n");
Console.WriteLine("Hello how are u");
}


<!-- code for Hello.html -->
|<HTML>
<form action ="/cgi-bin/Hello.exe">
<BR><BR>
<center>Click The Button<br>
<input type=submit value="Show Message"></center>
</form>
</
HTML>

Example 2: Printing Ascii values

The following program prints the ascii values on to a web browser. Place the Ascii.exe in cgi-bin or scripts after compiling Ascii.cs. Place the Ascii.html either in htdocs directory or wwwroots directory depending on the web server you are using. Open the browser by typing http://localhost/Ascii.html and click Show Ascii Values button to get the Ascii values.

//csc Ascii.cs
using System;
class Ascii
{
public static void Main()
{
Console.WriteLine("Content-Type:text/html\n");
for(int i=0;i <=255; i++)
{
Console.WriteLine(i + " " + (
char)i + " ");
}
}


<HTML>
<form action ="/cgi-bin/Ascii.exe">
<BR><BR>
<center>Click The Button<br>
<input type=submit value="Show Ascii Values"></center>
</form>
</
HTML>

Example 3: Fetching values from Database

The following program uses OLEDB SQL Servers native data provider and connects to pubs database. You may change Provider,server, uid and pwd values depending on your machine configuration. This program pulls the data from authors table and throws the output to a browser. Place the WebDb.exe in cgi-bin or scripts after compiling WebDb.cs. Place the WebDb.html either in htdocs directory or wwwroots directory depending on the web server you are using. Open the browser by typing http://localhost/WebDb.html and click Show Table button to get the values of authors table.

//csc Webdb.cs /r:System.Data.dll
using System;
using System.Data.OleDb;
class selectDB
{
public static void Main()
{
string location = "Provider=SQLOLEDB;" + "server =(local);" +
"uid=sa;pwd=;" + "database=pubs";
OleDbConnection con =
new OleDbConnection(location);
con.Open();
OleDbCommand cmd;
cmd=
new OleDbCommand("select * from authors",con);
OleDbDataReader dbReader = cmd.ExecuteReader();
System.Console.WriteLine("Content-Type:text/html\n");
while(dbReader.Read())
{
for(int i=0; i<dbReader.FieldCount; i++)
Console.WriteLine(dbReader.GetValue(i)+ " " );
Console.WriteLine("<br>");
}
}


<HTML>
<form action ="/cgi-bin/webdb.exe">
<BR><BR>
<center>Click The Button
<input type=submit value="Show Table">
</center>
</form>
</
HTML>

Example 4: Formatting the Database values

The following program uses OLEDB SQL Servers native data provider and connects to pubs database. You may change Provider,server, uid and pwd values depending on your machine configuration. This program pulls the data from authors table and throws the output to a browser. Place the WebDisplay.exe in cgi-bin or scripts after compiling WebDisplay.cs. Place the WebDisplay.html either in htdocs directory or wwwroots directory depending on the web server you are using. Open the browser by typing http://localhost/WebDisplay.html and click Show Table button to get the values of authors table. The only deviation from above programs is in the formatting where we have used table tags to format the output. 

//csc WebDisplay.cs /r:System.Data.dll
using System;
using System.Data.OleDb;
class selectDB
{
public static void Main()
{
string location = "Provider=SQLOLEDB;" + "server =(local);" + "uid=sa;pwd=;" + "database=pubs";
OleDbConnection con =
new OleDbConnection(location);
con.Open();
OleDbCommand cmd;
cmd=
new OleDbCommand("select * from authors",con);
OleDbDataReader dbReader = cmd.ExecuteReader();
System.Console.WriteLine("Content-Type:text/html\n");
System.Console.WriteLine("<table border=1>");
System.Console.WriteLine("<tr>");
for(int i=0; i<dbReader.FieldCount;i++)
System.Console.WriteLine("<td>" +dbReader.GetName(i) + "</td>");
System.Console.WriteLine("</tr>");
while(dbReader.Read())
{
System.Console.WriteLine("<tr>");
for(int i=0; i<dbReader.FieldCount; i++)
Console.WriteLine("<td>" + dbReader.GetValue(i) + "</td>");
}
System.Console.WriteLine("</tr>");
System.Console.WriteLine("</table>");
}


<HTML>
<form action ="cgi-bin/webdisplay.exe">
<BR><BR>
<center>Click The Button
<input type=submit value="Show Table"></center>
</form>
</
HTML>

Example 5: Capturing user Input

The following program uses OLEDB SQL Servers native data provider and connects to pubs database. You may change Provider,server, uid and pwd values depending on your machine configuration. This program pulls the data from authors table and throws the output to a browser. Place the WebDisplay.exe in cgi-bin or scripts after compiling WebInput.cs. Place the WebInput.html either in htdocs directory or wwwroots directory depending on the web server you are using.

This program takes the table name from the user and then pulls the data from the database and displays it in a well-formatted way. We have to make use of System.Environment.GetEnvironmentVariable(QUERY_STRING) to capture the value of query string variables. Query string information is passed in the pair value combination. In order to separate the query string variable values we make use of split function.

Open the browser by typing http://localhost/WebInput.html , enter the table and click Show Table button to get the values in the table name specified by you.

//csc WebInput.cs /r:System.Data.dll
using System;
using System.Data.OleDb;
class selectDB
{
public static void Main()
{
string varEnvi;
varEnvi= System.Environment.GetEnvironmentVariable("QUERY_STRING");
string[] result;
char[] separator= new char[1];
separator[0]='=';
result=varEnvi.Split(separator);
string str;
str="select * from " + result[1];
string location = "Provider=SQLOLEDB;" + "server =(local);" +
"uid=sa;pwd=;" + "database=pubs";
OleDbConnection con =
new OleDbConnection(location);
con.Open();
OleDbCommand cmd;
cmd=
new OleDbCommand(str,con);
OleDbDataReader dbReader = cmd.ExecuteReader();
System.Console.WriteLine("Content-Type:text/html\n");
System.Console.WriteLine("<table border=1>");
System.Console.WriteLine("<tr>");
for(int i=0; i<dbReader.FieldCount;i++)
System.Console.WriteLine("<td>" + dbReader.GetName(i) + "</td>");
System.Console.WriteLine("</tr>");
while(dbReader.Read())
{
System.Console.WriteLine("<tr>");
for(int i=0; i<dbReader.FieldCount; i++)
Console.WriteLine("<td>" + dbReader.GetValue(i) + "</td>");
}
System.Console.WriteLine("</tr>");
System.Console.WriteLine("</table>");
}
}

<HTML>
<form action ="/cgi-bin/WebInput.exe">
<BR><BR>
<center>ENTER THE TABLE NAME
<input type = text name=tableName>
<input type=submit value="Show Table">
</center>
</form>
</
HTML>

Conclusion

Writing applications, which run on Apache and IIS, require fair understanding of CGI programming. In this article I have dealt with fundamental concepts such as installing, configuring and deploying simple applications. However, a lot more is involved in CGI programming, like usage of cookies, response object, request object etc, which I shall explain in the coming articles. For example you may use different languages to develop applications, study performance issues, efficiency in threading models, calling components and others. Stay tuned for further articles in the same section.

Up Next
    Ebook Download
    View all
    Learn
    View all