ASP.NET image

ASP stands for Active Server Pages, it's an open source web standard developed by Microsoft which can be used to enhance websites and / or build full webapplications.

It used to be a stand alone environment ("ASP") but has become part of Microsoft's .NET framework. This framework is fully integrated in the Windows environments and provides a wide variety of development options. Amongst which ASP.NET, solely aimed at building web-based applications.

At first I started using it because I needed to, but now I'm also using it because I want to. I actually came to enjoy the environment and the massive freedom it provides quite a lot. SO, an advocacy page was required and here it is..

This page was last updated on: 25 Nov 2016 .

From Java Enterprise (EE) to ASP.NET?

This is what's puzzleing some of my friends; why would I completely give up on the working solution which Java EE provided and instead embrace a semi-open programming environment? This is something actually not that easy to explain, because a lot depends on "seeing is believing", even though this sounds extremely clichéd. The main aspect is that as soon as you dive a little bit into ASP.NET, especially from a Java background, you'll notice that ASP.NET managed to deliver that which Java has only been able to promise us so far.

Worse: ASP.NET managed to deliver yesterday.

I'm not trying to bad mouth Java here, that language is still my first love after learning how to program using Pascal. But the thing is; although Sun has always portrayed Java as a "network language" it falls short in comparison to other environments, especially when we're dealing with web based solutions.

Every web page is a class

The first thing I liked about ASP.NET is that it provides a fully flexed OO model which can be used for the web. So now we're not so much dealing with webpages ('contents') which get some business logic "attached" to them as is the case with environments such as PHP and Java EE. Instead all webpages are separate classes. All the elements on such a page can be either plain HTML elements or ASP.NET controls. However, a regular HTML element can also become a class element of its own which you can then access from within your code.

For example, let's say you set up a page which has a <div> element in it, and we want to dynamically add a link to this section in our code. In JSP your only option is to create a static HTML file (JSP file) and add a reference to your code (a JavaBean for example) and tell it to spit out the required HTML markup. Or you could use a so called scriplet; this is basically an in-line block of code. But either way you can't treat a <div> section as if it was an object of its own, it's only an HTML container.

But you can with ASP.NET. Here I'd only need to make sure that the <div> section gets parsed by the application server (IIS), I do that by adding a runat="server" declaration. Next I also need an identifier of some sorts, so I'll also add an ID tag, thus creating this HTML code: "<div ID="section" runat="server" />"

Now the only thing I need to do in my code section is create a new URL and then add that to my <div> section, for example like so:

HyperLink link = new HyperLink();
link.Text = "Click this link!";
link.NavigateURL = "http://catslair.org/";
section.Controls.Add(link);

It really is that simple. This gives you an amount of flexibility which is simply unheard of when using Java EE. In my markup (HTML) I can concentrate on contents (maybe I also want to add a specific style to the <div> section?) and in my code I only need to worry about the programming logic.

ASP.NET is its own CMS system.

Now, this isn't entirely true, but because ASP.NET provides so many pre-made building blocks (called server controls) which you can often use "out of the box" it sure feels this way. And it's not just building blocks either: what to think about building and applying website templates?

Ever since "website builders" became more popular (here's looking at Dreamweaver for example) they started to provide an option to build templates. Templates are basically page layout definitions which can be used to make a clear separation between the website layout and the contents of a single page. This can make it really easy to set up a main layout once, after which all the upcoming separate pages also use that same main layout. The main problem though: this technique was often tied to the design program itself. If you started using something else you could rebuild your template designs from scratch again.

The template technique is also often used in programming environments such as Java EE or PHP. But unfortunately these languages can only mimic the behaviour by applying include constructions. Or, when looking at PHP, people started developing code to enforce a certain templating standard. The so called template engines.

But the main problem with all those solutions is that in the end it's still a hack. if you encounter the 'require_once("mainpage.php")' option in a PHP file then it's immediately clear that mainpage.php is a template of some sort. But will that be just as obvious if you start looking into mainpage.php first? I doubt it.

ASP.NET on the other hand allows you to create master pages which can define content sections. You can then create content pages which will reference the master page and provide the actual contents for all the previously defined content sections. The advantage should be obvious: a content page only needs to contain a very limited amount of code which references the master page. Next you need a definition for all the content sections which have been defined in said master page and you're done. All that's left to do is provide the actual contents.

Not only does this make it easy to separate between content and design, it can never become a puzzle which content page(s) belong to which masterpage. There is certainly no need for cryptic comments.

Code separation

One of the main reasons why stylesheets were invented was to create a separation between contents and design, and in a way its reasoning is closely related to the concept of using master pages ("templates"). If several of your HTML pages use the same CSS stylesheet then it will be very easy if you need to change the overal text color or its size. You only need to make one change in the stylesheet and you're done.

ASP.NET took this aproach even further by adding code separation. One the of the specific advantages of C#.NET and VB.NET is that they provide support for so called partial classes. This basicaly means that you can define a class within several files.

And although the webpage (.aspx file) is the main part it can now reference a so called "code-behind" file which is basically a code file which provides the definition for a partial class. This approach allows you to fully focus on webcontents in your ASPX file while you can put all the programming logic in this "code-behind" file.

And because a webpage is basically a class of its own (System.Web.UI.Page) it also provides several methods which you can override. For example OnLoad() which gets called when your page is being loaded by a client. Or OnInit(); this takes you one step back and provides control over how the class (webpage) is being initialized.

During this stage you could dynamically change specific parts of your webpage. For example by adding a CSS style definition:

protected override void OnInit(EventArgs e) {
   base.OnInit(e);
   this.Header.Attributes.CssStyle.Add(HtmlTextWriterStyle.Color, "red");
}

That is the kind of freedom you have when working with ASP.NET.