Web enable a C# program

Step-by-step instructions for creating a Web-enabled program in C#.

Let other users know how useful this tip is by rating it below. Got a tip or code of your own you'd like to share? Submit it here!


First we need a Web server. Apache is the most popular, practical and easily configurable Web server available on the Internet. It can be downloaded from www.apache.org. Apache runs on most operating systems. This will also work on IIS.

After installation on the network drive or local drive, we see a number of subdirectories among which we see cgi-bin and htdocs. The cgi-bin subdirectory is used to contain executable files, while htdocs is used to contain HTML files. When your installation and setup configuration is successful (for help see the Web site), type the address as http:/127.0.0.1 and press enter. We see a default page placed in the htdocs directory and see the apache server running. Change the htdocs subdirectory and create an HTML file new.html to display a form page as follows:

<html>
<form>
<input type=submit value=search>
</form>
</html>

Now save this file! Open up your favourite browser and type http://127.0.0.1/new.html, then press enter. We see results! A button with a text (caption) search, is on the page.

Now we get down to business. Let us try out a C# code snippet that, when a user clicks on the search button, a C# program loads and executes.

Make a file new.cs and write the following lines into it:

class test
{
public static void Main()
{ 
System.Console.WriteLine("hi <b>it's me");
}
}

Save this file. Using csc.exe compile it, turn it into an .exe, and put it in proper Apache's cgi-bin subdirectory, or allow execute permissions if using IIS.

Now, for this, we need to modify a few things to our new.html, as follows:

<html>
<form action=http://127.0.0.1/cgi-bin/new.exe>
<input type=submit value=search>
</form>
</html>

We open up the browser type in the URL http://127.0.0.1/new.html. We see what? Not "hi it's me," but an "Internal Server Error"! Do not panic. Our new.cs code just needs slight modification too! See the new code below:

class test
{
public static void Main()
{
System.Console.WriteLine("Content-Type:text/htmln");
System.Console.WriteLine("hi<b>it's me");
}
}

Now if we execute it the same way into the browser, we see no errors. Yes! You have just successfully, without spending even a quarter of an hour, made a Web-enabled program in C#.

Last but not the least, here is something for better understanding. Note the code below:

class test
{
public static void Main()
{
System.Console.WriteLine("Content-Type:text/plainn");
System.Console.WriteLine("hi<b>it's me");
}
}

In the content-type, instead of HTML, I changed it to plain. So what does that output to the monitor? Don't be surprised. It won't flash an error. Rather, forthcoming lines containing HTML tags won't take effect and will behave as simple text.

Hope you didn't find the reading time hard on you! Happy coding.


Source: DotNetExtreme.com
This was first published in December 2002

Dig deeper on ASP.NET development best practices

0 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

-ADS BY GOOGLE

SearchCloudComputing

SearchSoftwareQuality

SearchSOA

TheServerSide

SearchCloudApplications

Close