Archive for September, 2006

The trouble with ASP.Net

Wednesday, September 27th, 2006

ASP.Net was a huge step up from ASP classic with it’s nasty non-OO, non-typesafe, spaghetti code limitations, but it’s not all good. It’s easy to create a site in ASP.Net that search engines, browsers and users all hate. Here’s a few thoughts on avoiding some of the pit falls.

The fundamentals of HTML 

Like anything, it’s hard to do web development well unless you understand the fundamentals. Joel Spoesky wrote a great piece on the law of leaky abstractions. In it he talks about the way that ASP.Net tries to make web development more like VB development…you create a blank form, add some controls, and double click to add functionality, just like VB! But the web is not VB, and that’s the first trouble with ASP.Net.

To understand why ASP.Net can get you into trouble, you need to understand the fundamentals of HTML, and I really mean the fundamentals.

  • What’s a URL?
  • What’s a query string?
  • How do you use the query string to pass information from one page to another?
  • How long can a query string be?
  • what’s a FORM?
  • What does the ‘METHOD’ attribute do on a FORM? What’s the difference between METHOD=’GET’ and METHOD=’POST’
  • What’s a hidden form variable?
  • How many forms can be on a single HTML page?
  • What’s a cookie? What are they used for?
  • What’s the HEAD section? What should be in there?
  • What are the following tags HTML tags? H1, H2, P, A, OL, UL, LI, DL, DT, DD, DIV, STRONG, BR

The nasty postback 

The first, and nastiest thing that ASP.Net does that causes trouble, is that it tries to make web pages event driven. It does this, using a clever series of javascript files that can found in the ASPNET_Client directory. Aside from the obvious limitations of browsers with javascript disabled, the biggest problem with this approach is that it confuses search engines.

For example, if you build a menubar on your home page in ASP.NET that uses the standard controls, your site simply won’t be indexed. Spiders can’t follow the javascript postbacks that make the buttons work.

Same thing for grids, lists or any control that ends up taking you to another page. Avoid ASP.Net navigation controls in most cases, a simple html anchor works best for everyone.

The tendency to build everything on one page

Because the ASP.Net model is trying to be more like a VB application, it tends to make developers write web pages like VB forms with too much stuff happening on a single web form. The classic case is the master detail product list where both the product list and the product details are located on a single .aspx page.

Again, the problem with this approach is that it’s difficult for search engines to spider all the contents of the page, and it’s difficult to bookmark the product detail, or send a link to a certain product to a friend.

Of course from the programmers point of view, it’s a snap, because you don’t have to maintainstate between pages, it’s all done for you.

The tendency to use controls to set up your page template

Not sure if this is a trouble with ASP.Net or a trouble with developers, but I’ve seen lots of web sites where the page header is extracted into a user control, including the whole HEAD section, TITLE and META tags. Unless done correctly the result of this is that all pages have the same TITLE and META tags. Search engines hate this, and it makes the back button, history and bookmarks  difficult to distinguish.

The way forward

First let me say again, that ASP.NET is a huge advance over ASP classic. But, like many things, if you use it the way the manual suggests, you’ll end up with a pretty poor excuse for a website.

To get the most out of ASP.NET, put it away for a while, pull out notepad or php (or even asp classic), and write a simple website using pure HTML fundamentals. Treat each page seperately, pass information via query strings or forms, create TITLE and META tags for every page, don’t use javascript.

Put this site on the web, and see how it rates in the search engines. See how easy it is to bookmark, then apply what you’ve learnt to your next ASP.NET project.