March 5, 2021
Hot Topics:

What is ASP.NET?

  • By Sonu Kapoor
  • Send Email »
  • More Articles »

Part II: The Practical Part

After you have learned the theoretic basics of ASP.NET, we can now move to the practical part! In this part you will see you how easy it is to create dynamic web pages. Before creating an ASP.NET page, you will need to create a Web-Application. A Web-Application is a Web site which can be of an enterprise or of something else. A Web-Application consists of two directories, a physical directory on your HD on the other side a virtual directory on your Web server. This virtuall directory is linked to the physical directory. To create a Web-Application, you must create a directory in c:\inetpub\wwwroot\ and name it however you want, for example asp.net. After you have created the physical directory, you must create the virtual one. For that you need to go


Select the "standard website" press the right mouse button and select New | Virtual Directory from the context menu. The wizard will guide you through the rest of the process. You can choose any alias name and under this name you can call your Web page. For our example we will also use the alias asp.net. In the second step, select the physical directory which we had created. By doing this we are linking the virtual directory with the physical one. In the last step you can choose the access rights for the directory. The options read and execute script are already selected, for further tests we will also select browse. After you have created all necessary directories, you can call the virtual directory via the browser. Enter the following URL in your browser:


After the protocol "http" you have to enter the name of the server localhost and then the directory of the Web-Application, in our case asp.net. Because we have selected the option browse, you will see a directory list, but as your directory is empty, you won't see any file.

NOTE: Of course you can also work without virtual directories, but I do not recommend to do so. The difference between working with virtual directories and without is that you always have to copy your folder/files in the subdirectory of c:\inetpub\wwwroot\. For example create a directory called test and copy some asp.net files in that folder. Now you can call this files via entering the following URL in the browser:


Your first ASP.NET page

An ASP.NET page is nothing more than a text file with the extension .aspx. According to this extension the IIS (Internet Information Services) recognizes an asp.net Web page and sends it further to the ASP.NET engine. You can try this easily; create a file called helloworld.aspx in the directory c:\inetpub\wwwroot\asp.net\. Enter any text you like in the file for example: hello world!. Now you can refresh your page in the browser and you will see the file helloworld.aspx in the list. If you select the file, you will see the output in the browser.

However its not a big deal to display a static text in a browser. Now let us use a little bit of ASP.NET to create a dynamic page. We want to display the same text, but this time we want to display it programmitacally.

helloworld.aspx - C# Code:

01: <% @Page Language="C#" Debug="true" %>
02: <script runat="server">
03: void Page_Load(object sender, EventArgs e)
04: {
05:     Response.Write("Hello World!");
06: }
07: </script>

In the above code you can see the <script> tag which you might know from client side languages like JavaScript, but you can also see the attribute runat="server" which is used to execute the whole code on the server side. In line 3 you can also see the event Page_Load which is executed on each loading of the browser. The function/method write in line 5 is called from the Response object, which is basically used to display any text on the browser. You can pass any text in the brackets or you can also use any variable, both will work. Although this code is dynamic, the result is the same as the static page. So we dont see much of the real dynamic effect. So let us take an another, more enhanced, example. As already promised in Part I, I will show you the code for greeting somebody according to the day-time. First we will calculate the daytime and according to that we will display some text. Create a new file in the directory c:\inetpub\wwwroot\asp.net called Greeting.aspx and enter the following code.

Greeting.aspx - C# Code 

1: <% @Page Language="C#" Debug="true" %>
2: <script runat="server">
3: void Page_Load(object sender, EventArgs e)
4: {
5:     int hour = DateTime.Now.Hour;
6:     if( hour <= 11)
7:     {
8:         Response.Write("Good Morning.");
9:     }
10:   else if( hour <= 18)
11:   {
12:       Response.Write("Good Afternoon.");
13:   }
14:   else if( hour <= 20)
15:   {
16:       Response.Write("Good Evening.");   
17:   }
18:   else
19:   {
20:       Response.Write("Good Night.");   
21:   }
22:   Response.Write("<br>"+DateTime.Now.ToString());

If you execute the above shown code in your browser at 12:00PM, you will see a message "Good Afternoon" and if you call it in the evening you will get an another message. So this code is much more dynamic as the previous one. 

In this and the first displayed code you have surely recognized the first line <% @Page Language="C#" Debug="true" %>. These are ASP.NET directives, which are used for the compiler and configuration. You could paste this line anywhere in the code, but usually they are placed in the first line. The Page_Load is one of the inbuild functions but you can also define your own functions. Here an example:

HelloWorld1.aspx - C# Code:

01: <% @Page Language="C#" Debug="true" %>
02: <script runat="server">
03: void Page_Load(object sender, EventArgs e)
04: {
05:     HelloUser();
06: }
07: void HelloUser()
08: {
09:     Response.Write("Hello World");
10: }
11: </script>

As you see, we have created the function HelloUser(). This function is called within the Page_Load function. Instead of C# you could also use VB.NET. Here is the code in VB.NET.

HelloWorldVB.aspx - VB.NET Code:

01: <% @Page Language="VB" Debug="true" %>
02: <script runat="server">
03: Sub Page_Load(sender As Object, e As EventArgs)
04:     HelloUser()
05: End Sub
06: Sub HelloUser()
07:     Response.Write("Hello World")
08: End Sub
09: </script>


In this Article I have shown you the theoretic and practical basics of ASP.NET. I have shown you how easy and how few code lines you need to create an ASP.NET page. The examples were written mostly in C#, but one example is also written in VB.NET.



About the Author:

Sonu Kapoor is the founder of www.CodeFinger.de. He has written many articles and applications for various companies. He writes articles as a freelancer for Developer.com and can be contacted at sonu@codefinger.de. If you have any comments, suggestions, or any questions regarding this article, feel free to contact Sonu.

Page 2 of 2

This article was originally published on February 20, 2003

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date