Microsoft & .NETASPWhat is ASP.NET?

What is ASP.NET?

Developer.com content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

Introduction:

In the past I worked very much with Active Server Pages (ASP) and everything was fine.
Microsoft has since released a new technology called ASP.NET. In this Article I will try to explain the basics of ASP.NET. I will not only explain the theoretical part but I also will show you some practical "hello world " style examples. For these examples I will use C#, and VB.NET. I will try to answer all questions as good as I can. So take a cup of tea, have a seat on your favorite place and enjoy this article.

System Requirements

According to Microsoft it’s sufficient to have a 133 MHz system with 128 MB of
RAM to use ASP.NET. For a better performance I recommend using at least Pentium II or III with 500 MHz and minimum 256 MB of RAM. You will also need the following software installed:

  • Microsoft .NET Framework SDK ( Download
    from Microsoft
    )
  • Microsoft Windows Component Update
  • Internet Information Services 5 or 6
  • Windows 2000 Professional with Service Pack 2 or
  • Windows 2000 Server with Service Pack 2 or
  • Windows XP Professional or
  • Windows Server 2003

Note: You can develope ASP.NET pages on Win98/ME but to run these you
will need a system with IIS 5 or 6.

Part I: What is ASP.NET?

With HTML you can only create static web pages, which used to be very useful and sufficed the needs in most cases. But as the Internet grows daily, and so do the demands of its users, you will soon need the ability to create dynamic pages, that react according to the action of the
user. This, and much more, is possible with the help of ASP.NET.

ASP.NET can be used to make the Internet appear dynamical, faster and much more effective. Imagine you have a user called "Smith". You could write a dynamic ASP.NET page, which will greet the user according to the current time. In the morning, let’s say from 6:00 AM to 11:00 AM you could greet Mr. Smith with something like: "Good Morning Mr. Smith. How are you ?" and from 12:00 PM to 18:00 PM with something like: "Good Afternoon Mr.Smith" and so on. I will show you the code for this example later.

In ASP.NET, similar to desktop applications, you can use variables, define functions and make use of a database, even a connection to a remote database is possible. In summary, ASP.NET is a programming system used to develop dynamic Web pages. This system provides the necessary
technologies, like classes and objects, but yet it is not a programming language per se.

How does ASP.NET work?

For a better understanding on how ASP.NET works, I will first show you the traditional way that a Web page works (without ASP.NET).

1: Any internet user ( client ) is asking for
internet-page.
2: The Web-Server sends the physical content
of the page back.

As you can see in the above image, calling a static page is very simple process. A
client is asking/demanding for a Web page. For that you will need a connection
between your client ( IE, Netscape etc.) and the server – this is done through the
Internet. The file must exist on the server otherwise you will get a “404 File
not found” error. The server reads the requested file and sends it back to the client.
It doesn’t matter who the client is or when the request reaches the server, the result will always be the same – until the file on the server is modified.

Now let us see how this will look with the usage of ASP.NET:

1: Any internet user ( Client ) is asking for
internet-page.
2: Server sends the inquiry to the ASP.NET engine.
3: ASP.NET creates the required new webpage
for the user.
4: ASP.NET sends the created page back the Client.

The request of a dynamic Web page differs much from the static one. After the server has received the inquiry, it will be send to the ASP.NET engine. This checks whether the requested file exists. If the file exists, ASP.NET will not simply send the content back, instead it will create a new dynamic page that will be send back to the client. This dynamic page can look different from user to user.

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:inetpubwwwroot 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

Start->Settings->System->Administration->Internetservice-manager.


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:

http:localhostasp.net

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:inetpubwwwroot. 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:

http:localhosttestanyfile.aspx

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:inetpubwwwrootasp.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:inetpubwwwrootasp.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());
23:}
24:</script>

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>

Conclusion:

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.

Download:

Examples.zip

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.

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories