04
12/10
Weaning Yourself Off of Visual Studio (Part 3)
This is the third and final part of my series on how to not use Visual Studio. Microsoft does a pretty good job abstracting the creation of boilerplate code away from you with Visual Studio, to the point that it’s fully possible to create .NET applications (both desktop and web alike) without actually knowing anything about this boilerplate code, or what it does.
As far as I’m concerned, knowing about this code, how to write it, and what it does, and being therefore able to not use Visual Studio should the need arise, enriches your understanding of the .NET Framework, and ultimately makes you a better .NET developer.
Today I’m going to talk about ASP.NET, and how to create ASP.NET pages without using Visual Studio.
The first important thing about creating ASP.NET pages that’s important to understand, is the idea of an “ASP.NET application“. An ASP.NET application is a folder on the IIS web server that has been converted—by way of the IIS management MMC—into an application. Only within ASP.NET applications may ASP.NET pages et cetera run. When using things like HttpServerUtility.MapPath() (referred to from within Pages etc. as “Server.MapPath()” due to the fact the HttpServerUtility of the current HttpContext is pointed to by the Server property of Pages), the root directory of the current application is referred to as “~” (i.e. tilde).
Each ASP.NET application has at least one important directory at its root: “bin“. Within this directory, all *.dlls for the application must be placed. You can put pages etc. in any subdirectory, any number of subdirectories deep, but anytime a page requires *.dlls these *.dlls must be in ~/bin/, where “~” is—again—the root of the application. To elaborate, whenever a page—an *.aspx, for example—is compiled, it is linked to all the *.dlls in ~/bin/.
The above is important because you may wish to create *.dlls to use across multiple pages, and because code-behind files (i.e. *.aspx.cs) must be compiled to *.dlls and placed in ~/bin/ for their corresponding *.aspx to compile properly.
Depending on the complexity of the page that you’re creating, you’re going to be looking at one or more files. The first file will be the *.aspx file, and the second will likely be a *.aspx.cs file (i.e. the code-behind file), and the others will likely by *.cs files that you create just to split things up nicely (this would be a good time to laugh in Java’s face because it doesn’t support partial classes (along with many other things…)).
You’ve probably gathered this from using Visual Studio, but just as a recap, the *.aspx file contains normal HTML (i.e. HTML, JavaScript, CSS, etc.) alongside ASP.NET mark-up, which is XML-style tags that begin with “asp:“—for controls—as well as tags beginning with “<%” and ending with “%>”—for directives and in-line code.
The first line of an *.aspx page should always be a directive which tells ASP.NET what kind of page it is. Directives are contained by “<%@” and “%>“. For *.aspxs this will—at minimum—look like this:
<%@ Page Language="C#" %>
And if you’re doing anything remotely complicated, will likely look something like this:
<%@ Page Language="C#" AutoEventWireUp="true" CodeBehind="example.aspx.cs" Inherits="examplepage" %>
Each of these attributes does something different:
- Page – Tells ASP.NET that this is a page. Some other options here are “Handler” and “MasterPage“.
- Language – Tells ASP.NET what language this page’s code blocks will be written in. Can be “C#” or “VB“.
- AutoEventWireUp – Allows you to handle events with specific naming conventions. Such as a function Page_Load for when the page is loaded. Not really necessary, but could be useful. This is—and should be—set to “false” when working in Visual Studio, since Visual Studio automatically wires events itself (through automatically-generated designer code), and does not need the Framework wiring the events up again.
- CodeBehind – Specifies the name of the code-behind file.
- Inherits – Specifies what class the page inherits from. More on this later.
Using what I’ve already told you, you can already create ASP.NET pages that do simple things, using in-line code using “<% [...] %>“.
This, for example, is an ASP.NET page which, when saved as a *.aspx and placed on an IIS web server in an application, will display “Hello, world!” (original, I know):
<%@ Page Language="C#" %> <html> <head> <title>Hello, world!</title> </head> <body> <% Response.Write("Hello, world!"); %> </body> </html>
But this isn’t too useful. In order to write bigger, better, and more complicated pages etc., we’re going to want to separate the *.aspx and the code. We already know how to point an *.aspx at a code behind file, and specify what it inherits from, let’s discover how that all fits together.
The first thing to understand, is the idea of “Inherits“. This is literal—the code infront page doesn’t represent an extension of the class it “Inherits“, it very literally inherits from that class. This is important because it means that methods etc. that you want exposed to the code infront page are going to have to be protected, not private (if you aren’t an accessibility level Nazi like me, and just make everything public, then you need not worry yourself over this detail).
Once you understand this, you need to understand that supplying the “Inherits” directive doesn’t make the page start inheriting from something, it makes it inherit from something other than the default. By default pages inherit from System.Web.UI.Page, so when writing your code behind file, you need to make the class you define inherit from this.
Another thing to understand, is that if you want to access controls from the code behind file, they need to be declared—if you root around, you’ll find that Visual Studio does this in its designer code file. Their name needs to be the same as the “id” tag in their ASP.NET markup—best practice is to declare them as protected. If you don’t need to access them from the code behind, you don’t need to declare them (or even give them an “id” tag for that matter).
As long as a method in the code-behind is protected or public, you can access it from in-line code in the code infront file just as you would any other function (useful for cutting long in-line code blocks down—just turn them into one method call and put the rest of the code in the method in the code behind file).
Once you’re done with your code-behind file, use csc.exe to compile it to a *.dll, and put it in ~/bin/. If you don’t do this, the Framework won’t be able to find the class your *.aspx inherits from, and will become angry.
If you want to see how it all works, I wrote a “URL Lengthener” for fun last night (it’s takes those annoying bit.ly, etc. links and tells you where they really point by following the trail of 3xx codes) using the methods I’ve outlined in these part few articles (i.e. not using VS):
- The application itself, compiled, hosted, and running.
- The source for the *.aspx (i.e. lengthener.aspx).
- The source for the code-behind (i.e. lengthener.aspx.cs).
As usual, copyleft (on the source), et cetera. 






