Monthly Archives: February 2010

Asp.NET Development With Nothin’ But a Pair of Sticks

Given my status as a Microsoft Certified Technical Specialist, I figured I should start playing with .NET and – since I fancy myself a bit of a web-developer – Asp.NET in particular. When it comes to Asp.NET though I don’t really have a good environment in which to do anything cool like take chances, get messy or make mistakes and since my work environment provides me with all the conveniences of Asp.NET development, I thought maybe I’d like to contrast that a little at home by learning this the way Boy Scouts stereotypically learn to make fire.

Setup

With a little bit of what some might call researching, I figured out that I am running "Windows Vista Home Premium" (Start>Computer>Help>’About Windows’) which means that IIS and Asp.NET are waiting for me to turn them on. All I have to do is activate those Windows features from the Control Panel.

Turn Windows Features On-Off

If you’re following along and running Vista, while you’re in there you might want to consider poking around and choosing if you want to remove some features which may just be slowing your machine down. Most notably – since this exercise involves a lot of ‘Run as Administrator’ – you might want to turf off UAC.

Activating the features took "several minutes to complete", but once I was done I had IIS and Asp.NET up and running.

I was surprised to discover that the setup was that easy. Seriously, try it; once you do the above, just type up an ASPX file… (I did it in notepad and cleverly named the file "hello.aspx")

<%@ Page language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
	<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
	<title>Hello World AspX</title>
 </head>
 <body>
  <form runat="server">
	<p>
		Did it work?
		<asp:label id="message" runat="server" Text="Yes, it worked!" />
	</p>
  </form>
 </body>
</html>

… and drop it in the C:\inetpub\wwwroot folder, no different than you’d toss a PHP script in an htdocs folder on a LAMPP system. Open up a web browser and visit http://localhost/hello.aspx and you should see the fruits of your labors!

Again, if you’re following along and this is your first aspx file, you should take note of the characteristics that distinguish ASP.NET Web pages from static HTML pages:

  • A file name extension of .aspx instead of .htm, .html, or other file name extensions. The .aspx file name extension causes the page to be processed by ASP.NET.
  • An optional Page directive. In the future, you might use other directives, such as Control, Import, Implements, Register, Assembly, OutputCache and Reference.
  • A <form> element which includes a runat='server' attribute.
  • Web server controls, such as the <asp:Label>. (note that it also has a runat='server' attribute) These are special tags which are processed server-side and are changed into (sometimes complicated and inaccessible) HTML-CSS-JavaScript.
  • Server code, though this is not in the example.

Adding Code Behind

As exciting as accomplishing this much was, the really useful part of Asp.NET development (contrasted with classic Asp) is the code behind. (this is also where JSP disappoints a little…1)

If you’re not familiar with what I mean by code behind, support.miscrosoft.com describes it very effectively as follows:

Code-behind refers to code for your ASP.NET page that is contained within a separate class file. This allows a clean separation of your HTML from your presentation logic.

I wanted to write up a code behind, still using no spiffy development environments. This meant three things:

  1. Writing the code behind
  2. compiling the code behind to a DLL
  3. deploying the compiled DLL to my web application

Writing a C# Code Behind

Writing a C# code behind the old fashioned way using just notepad and the command line wasn’t that tough in the end. All I had to do was install the Windows SDK, open up notepad and write out a class (in C#) which extends System.Web.UI.Page:

using System;
	
public class HelloWorld : System.Web.UI.Page {
	
	protected System.Web.UI.WebControls.Label message;
	
	public void Page_Load() {
		message.Text = DateTime.UtcNow.ToString();
	}
	
}

This code will change the Text of a ‘message’ Label to the current date-time. I’m doing this by overriding the Page_Load method of class System.Web.UI.Page and manipulating my server controls in that method. Overloading this method actually seems to be a shorthand for using the Load event:

using System;
	
public class HelloWorld : System.Web.UI.Page {
	
	protected System.Web.UI.WebControls.Label message;
	protected System.Web.UI.WebControls.Button button;
	
	public HelloWorld() {
		this.Load += LoadHelloWorld;	// I like this better than using Page_Load
						// but if you're new to .NET Page_Load might be easier to grasp…
	}
	
	public void LoadHelloWorld(Object sender, EventArgs e) {
		message.Text = DateTime.UtcNow.ToString();
	}
	
}

… whichever way, the end result is the same.

Next came the interesting part; I saved the file as ‘HelloWorld.cs’ and compiled it into a DLL.

Compiling a .cs File On The Command Line (Look Mom! No IDE!)

To use this HelloWorld class in an Asp.NET page, it had to be compiled into a DLL. Again, though, the point of this exercise has been to learn how to develop .NET from the ground up using nothing but my wits and a couple of twigs, so I installed the Windows SDK on my computer, which includes the C# compiler csc.exe. The compiler itself ends up in the C:\Windows\Microsoft.NET\Framework\v3.5 folder, so from the command prompt I used the following command to compile ‘HelloWorld.cs’ into a DLL:

> C:\Windows\Microsoft.NET\Framework\v3.5\csc.exe /t:library HelloWorld.cs

The /t:library option specifies that the ouput file should be a DLL.

Since I suspsect I’ll be using csc.exe pretty often in my Asp.NET adventures, I think I’ll modify the PATH environment variable on my system to include the C:\Windows\Microsoft.NET\Framework\v3.5\ directory, so I don’t have to type out the full path to csc.exe every time I use it…

> csc /t:library HelloWorld.cs

Deploying the Code Behind

I copy-pasted the resulting ‘HelloWorld.dll’ file into the ‘bin’ directory of my web application’s virtual directory, C:\inetpub\wwwroot\bin and added an ‘inherits’ attribute to my Page directive:

<%@ Page language="C#" inherits="HelloWorld" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
	<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
	<title>Hello World AspX</title>
 </head>
 <body>
  <form runat="server">
	<p>
		Did it work?
		<asp:label id="message" runat="server" Text="Yes, it worked!" />
	</p>
  </form>
 </body>
</html>

Since this was my first little adventure with Asp.NET on this machine, the bin folder did not exist for my web application, so I had to create it myself.

Alternately, I figure I could have deployed to the GAC.

1 Why am I disappointed? JSP Page directives provide the ‘extends’ attribute but no one seems to have ever used it! The only information I’ve ever found about it is that "Sun recommends ‘to use this attribute cautiously’".