Blue Theme Orange Theme Green Theme Red Theme
 
Nevron Gauge for SharePoint
Home | Forums | ASP.NET 2.0 Tutorials | Web Services | How Do I...? | Class Browser | WPF Quick Starts | Advertise with Us
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Nevron Gauge for SharePoint
Search :       Advanced Search »
Home » Enterprise Development » Building .NET Application for Windows Apache Server

Building .NET Application for Windows Apache Server

In this tutorial 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.

Page Views : 7266
Downloads : 31
Rating :
 Rate it
Level : Intermediate
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
CodeApacheByNRS.zip
 
 
Nevron Gauge for SharePoint
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 

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.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Narayana Surapaneni
Narayana Rao Surapaneni, a Software Engineer working for Patni Computer Systems Limited, India is a Microsoft Certified Solution Developer and Sun Certified Java Professional. He also holds other certifications in ASP, E – Commerce Concepts, Visual InterDev etc.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
 Comments
6 Months Free & No Setup Fees ASP.NET Hosting!
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.