MSDN just published an article on "Building ASP.NET 2.0 Web Sites Using Web Standards" by Stephen Walther from superexpert.com. Whilst overall it's useful, it's not perfect, I want to take issue with 3 points it makes.

  1. Validating XHTML Pages

    The section on validating XHTML pages offers up the W3C validation service as an alternative to the internal VS 2005 validator. This is a great recommendation, but should not be considered an alternative, but a "must do". The internal VS 2005 validator looks at the xhtml as you type. It does not, and cannot check the output of an actual page. Why is this important? Well once you start binding data, or dynamically filling placeholders with xhtml your page contents change and your generated content is just as likely, if not more likely to spit out invalid xhtml. The only way to check this is to check the page as served, with your data in place, so you must use a validator that will go to a web site and grab the page as served, or one that will allow you to upload content for checking (if you're willing to do the leg work and view source / save or cut and paste into the validator). As I keep pointing out asp.net will not serve content correctly to the w3c validator until you add a custom browser file. I'm very surprised to see no mention of this, without the browser file any xhtml 1.1 content served to the validator will fail validation.

  2. XHTML and MIME Types

    The author's method for pushing application/xhtml+xml is bad, although the idea is good. The problem with the sample code, as it stands, is that any file which is processed by the asp.net runtime would default to being served as application/xhtml+xml unless the code handling that file type says otherwise. If, for example, you're using VS.Net's internal web server, every file gets served by the run time, so suddenly your CSS files are served with a mime type of application/xhtml+xml. The default IIS error pages would be served as application/xhtml+xml and would refuse to display in an xhtml browser because they're nowhere near valid. If you want to go down the global.asax route of tweaking the mime type then do some more checking;

    void Application_PreSendRequestHeaders(Object sender, EventArgs e)
    {
      if (Array.IndexOf(Request.AcceptTypes, "application/xhtml+xml") > -1 &&
        Response.ContentType == "text/html" &&
        Response.StatusCode == 200 &&
        !Response.IsRequestBeingRedirected)
        Response.ContentType = "application/xhtml+xml";
    }

    This checks for a few basics, such as the page being served is an html page, the page being served is actually successful and it's not in the middle of a redirect. This is by no means perfect, I'm sure there will be cases where the mime type will be stamped inappropriately but it's a better starting point than the same code in the article.

  3. Accessibility

    The author hammers home the goodness that is accessibility and the WAI standards, but then goes on to break one of the basic guidelines. The WAI guidelines state

    Tables should be used to mark up truly tabular information ("data tables"). Content developers should avoid using them to lay out pages ("layout tables").

    The examples in "Creating accessible web forms" use tables for layout. By skipping this accessibility guideline the article also misses out on problems with some of the asp.net 2.0 controls. Container controls, those that hold other controls such as the New User Wizard, are littered with tables for layout. Even with the templating facitility they offer some controls refuse to drop the outermost table. Whilst this may be valid xhtml it does not conform to the spirt of the WAI guidelines. Readers should also note that the built in accessibility checker in VS 2005 does not flag container controls as something to be checked but it will flag your own tables.

As a final thought even the validators get it wrong. The asp:Checkbox does not render the required attribute value thus serving invalid xhtml. The w3c validator doesn't catch this as invalid.

asp.net 2.0 makes it easier to deliver a web site which conforms to standards but it's not a given, you still need to think and check rigorously.