ASP.NET Basics (part 1): Nothing But .Net - Hard Choices (Page 4 of 8 )
With the ASP.NET environment all loaded and ready for action, let's jump into some code without any further ado. Pop open your favourite text editor and copy the following lines of code into it:
<%@ Page Language="C#" %>
<html>
<head>
<title>Hello, Choice!</title>
</head>
<body>
<%
// print some output
Response.Write("Choice. The problem is choice.");
%>
</body>
</html>
Save the file on your hard drive as "choice.aspx" in a folder named (say) "ASPBasics". It is important to note here that all ASP.NET files must have the .aspx extension.
Next, in order to view this page via the IIS Web server, you'll need to create a so-called "virtual directory" with the Internet Service Manager application and map it to the folder above. Call this virtual directory
(say) "ASPBasics" as well.
You should now be able to view the page created above by browsing to the URL http://localhost/ASPBasics/choice.aspx in your browser (in case you named the script or directory differently, or if your Web server is accessible by a name other than "localhost", you should make the appropriate changes to this URL). Here's what you should see:

Let's take a closer look at the code:
<%@ Page Language="C#" %>
This is the all-important "Page" directive, which must appear at the top of your ASP.NET program. Among other things, this directive is used to indicate the programming language used within the page. The default language for ASP.NET is VB.NET; however, I've chosen C# instead, for a number of different reasons:
1. It's the language specially developed for the .NET Framework;
2. It's completely object-oriented, thereby making it cleaner, easier to understand and a lot more fun to program in;
3. For VB developers, there is no "unlearning" required (most VB programmers would have to unlearn quite a few concepts before jumping on the VB.NET bandwagon);
4. I've always, always wanted to program in a language with a # in its name.
One way to include a block of C# code is to enclose it within <%...%> tags (similar to ASP and PHP code), like this:
<%
... some C# code ...
%>
You can also use the <script> element to include C# code - that's demonstrated in the next example.
So where's the C# code that I'm harping on, anyway?
<%
// print some output
Response.Write("Choice. The problem is choice.");
%>
As you might have guessed, this line of code simply outputs a line of text to the browser. Instead of "hello world", I decided to use a line from the latest installment of "The Matrix" trilogy. In case you haven't heard about "The Matrix"...it's a deeply-existential film about how the entire world is an illusion created by machines, humans are actually batteries powering the system, and a hero will rise to save us all from our fate. Woohoo!
OK, enough of the geek impersonation. Back to business...
Wanna have some fun? Remove the semi-colon from the end of the line in the example above and reload it in the browser. You should see the following ugly error message:

Yup, life in the .NET world is pretty rigid. But hey, it gets better from here!
Next: Command And Control >>
More ASP.NET Code Articles
More By Harish Kamath (c) Melonfire