|
Introduction:
In the past I worked very
much with ASP and everything was fine for me. But I thought why not try the
newer Technology of Microsoft called ASP.NET ( Active Server Pages ). 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. If you have any comments, suggestions, or any questions,
please feel free to contact me. 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. 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 .NET Server
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 makes the Internet 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, lets say from 6:00AM to 11:00 AM you could greet Mr. Smith something
like: "Good Morning Mr. Smith. How are you ?" and from 12:00PM to
18:00PM 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 a summary we can say
that ASP.NET is a programming system, to develop dynamic internet
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 is (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 webpage. 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 doesnt 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 forinternet-page.
2: Server sends the inquiry to the ASP.NET engine.
3: ASP.NET creates the required new
webpagefor the user.
4: ASP.NET sends the created page back the Client.
The request of
a dynamic webpage 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 *g*
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 webserver. 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
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 webpage.
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:\\localhost\asp.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:\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:
http:\\localhost\test\anyfile.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:\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());
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.
|