Bad HTML Can Crash ASP.Net?

ASP.Net, Browsers, XHTML/CSS Add comments

Yes, the title is a question. I think the answer to that is yes, although I am not sure.

I encountered a very strange problem yesterday and it took a while to track down. I found this problem so frustrating that for a moment (only a brief one) I questioned my adoration for ASP.Net.

What Was Happening?

I’ll explain a little about the problem so you can understand where I am coming from.

First off I should mention that this was occuring in an old in-house CMS product that, IMO, should be tagged as legacy code. It spits out bad HTML which is mostly to blame on a rich text editor that is used to design templates and modify text on pages. The pages have a HTML 4.0 Transitional doctype.

The problem was that ASP.Net Session variables were not persisting on page refresh. They were set and could be used afterwards in the same request but when I navigated to a different page or refreshed the same page all Session variables were null again.

To screw with my head a little bit I tested 3 different browsers. IE7 and Firefox 3 had the problem consistantly but Chrome worked just fine, my Sessions stayed alive from page to page. What the…!

The Solution…I Think

I eventually tracked the problem down after more than 4 hours of going through [legacy] code, line by line. That was painful. The way the CMS works is any template tags that are unused get removed so there isn’t a bunch of useless HTML messing things up. I found however that unused image template tags were still rendering but with an empty source attribute.

<IMG ALT="" SRC="" ONLOAD="constrain()">

Notice the all uppercase…gross. Anyway, I discovered that if the unused images were removed or the src was set to ’spacer.gif’ then all was well again.

The Take Away

So, I am left amazed that bad HTML code can mess with something like ASP.Net Session variables. How is this possible and why didn’t Chrome/WebKit have a problem? My first reaction would be to blame Internet Explorer but even Firfox choked on this!

And is leaving the src attribute empty even considered invalid markup (in HTML 4)?

This is by far the weirdest thing I have experienced while using ASP.Net. Has this happened to anyone else? Anyone care to explain how this is possible?

Did You Enjoy This Post?

Be sure to grab my RSS feed so you don't miss out on more great articles.

This Post Was Brought To You By

Get FREE Magazines Forever! Looking for Computer, Business, Engineering, and Trade magazines and articles? We've got 'em. Get yours now!

How do I save time? I use FreshBooks for invoicing.

Bad HTML Can Crash ASP.Net?

2 Responses to “Bad HTML Can Crash ASP.Net?”

  1. Martin Says:

    I think you are suffering from quite an odd problem here, but an empty image src in both Firefox and IE will cause either the current page (ff) or the root (IE) to be loaded again. You can see this if you look at the requests made across the browsers using Fiddler (www.fiddlertool.com) and Firebug (www.getfirebug.com).

    Notice that IE and FF both do additional requests when an image with an empty src is contained in a page, whereas chrome ignores them.

    Its possible that in this case the page_load or some part of the ASP.NET page lifecycle where you deal with session is being hit multiple times causing the problems you are seeing.

    Maybe this helped?

  2. Justin Says:

    @Martin, thanks for the suggestion. I’ll take a look and see what I can find.

Leave a Reply

WP Theme & Icons by N.Design Studio
Entries RSS Login