ARTICLE

Web Enabled C# Application

Posted by Kamran Articles | ASP.NET Programming January 02, 2002
C# is a sharp-edged weapon for Microsoft Visual Studio Programmers. My article beneath presents a brief eye-catching vivid view of programming internet web-browser using C#(C Sharp), and solves today's need with tomorrow's software!
Reader Level:

Summary:

C# is a sharp-edged weapon for Microsoft Visual Studio Programmers. My article beneath presents a brief eye-catching vivid view of programming internet web-browser using C#(C Sharp), and solves today's need with tomorrow's software!

Article:

First we need a web server. Apache is the most popular and practical and easily configurable web server available on the internet. It can be downloaded from http://www.apache.org. Apache runs on most of the operating systems.
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 website), 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 favorite browser and type http://127.0.0.1/new.html, and press enter. We see results! A button with a text(caption) search, is on the page!!!
Now come to business. Let us try out a c sharp code snippet such that when the 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.  What do we see?  Not "hi it's me" but an "Internal Server Error"! Panic not!  We have the exact solution to this. Our new.cs code needs slight modification too. See the new code below :

class test
{
public static void Main()
{
System.Console.WriteLine("Content-Type:text/html\n");
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 successfully without spending even a quarter of an hour, made first web-enabled program in c# .

Well, 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/plain\n");
System.Console.WriteLine("hi<b>it's me");
}
}

In the Content-type, instead of html, I changed it to plain. Well what is the output to the monitor? Don't amaze, it won't flash an error, rather forthcoming lines containing html tags won't take effect, and behave as simple text!

Login to add your contents and source code to this article
post comment
     

Just two days back I have started learning C# on my own and now I have changed this example so that the output screen showing the message was visible to me unless I hit ony key in the windows.
I used Console.ReadLine(); and at the top of program using System; and it's working....

Thanks for this post...
Regards,
Abhijeet

Posted by Abhijeet Patil Jun 23, 2009
COMMENT USING
PREMIUM SPONSORS
DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and add new content to existing PDF documents from within your applications.
Join a Chapter
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.