For as long as I can remember I’ve been making sure that the sites I play about with (published or not) produce markup that conforms with w3c specifications. The best-known way to do this is to run the markup generated by the web server through The W3C Markup Validation Service. Unfortunately it’s been difficult to make ASP.NET applications produce 100% valid markup, something that always frustrated me. There may have been ways to make this work before that I’m not aware of but here’s now to do it anyway.

It’s worth noting that the DOCTYPE declarations used throughout this article aren’t valid for HTML5.

I always write my sites to conform to XHTML 1.0 Strict specifications, despite currently making them HTML5-compliant wherever possible. If you’re unsure what this means I would recommend heading over the W3C website about XHTML 1.0. The Strict DOCTYPE is actually the way HTML and XHTML were designed to be used so I’d recommend using them over the older/legacy Transitional DOCTYPE. Depending on where you look or what you read, the transitional DOCTYPE is intended for sites making the transition towards standards-compliant markup. To me that sounds like pretty good reason to stick to the strict DOCTYPE.

Anyway, when you’re making your ASP.NET applications it’s possible to make Visual Studio generate applications that conform to the W3C recommendations and therefore work with the W3C Markup Validation Service. There are 2 things you need to do for this to work properly. Note that this isn’t specific to ‘standard’ ASP.NET applications – it will work with ASP.NET MVC applications, too.

Required steps

Step 1 isn’t strictly (no pun intended) required but it’s good practice. Edit Web.config and use the xhtmlConformance element to configure XHTML 1.0–conforming control rendering. Here’s what is in my Web.config files – this must be placed inside the section.

Web.config contents:

  <system.web>
     <xhtmlConformance mode="Strict"/>
     <!-- the rest of the <system.web> section has been removed for the purposes of this article -->
  </system.web>
  <!-- the rest of the <configuration> section has been removed for the purposes of this article -->
</configuration>

Microsoft’s MSDN page about the xhtmlConformance element can be found by going to http://msdn.microsoft.com/en-us/library/ms228268.aspx.