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.
I always write my sites to conform to XHTML 1.0 Strict specifications. 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.
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
1 2 3 4 5 6 7 | <configuration> <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.
Step 2 – the important one – requires that you create a folder in the root of the web application called App_Browsers and create a file in that folder called w3cvalidator.browser. The contents of this file are as follows.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | <browsers> <!-- Browser capability file for the w3c validator sample UA: "W3C_Validator/1.305.2.148 libwww-perl/5.803" --> <browser id="w3cValidator" parentID="default"> <identification> <userAgent match="^W3C_Validator" /> </identification> <capture> <userAgent match="^W3C_Validator/(?'version'(?'major'\d+)(?'minor'\.\d+)\w*).*" /> </capture> <capabilities> <capability name="browser" value="w3cValidator" /> <capability name="majorversion" value="${major}" /> <capability name="minorversion" value="${minor}" /> <capability name="version" value="${version}" /> <capability name="w3cdomversion" value="1.0" /> <capability name="xml" value="true" /> <capability name="tagWriter" value="System.Web.UI.HtmlTextWriter" /> </capabilities> </browser> </browsers> |
If this file isn’t present, for example, your ASP.NET application’s <form id="form1" runat="server"> may have an attribute called "name", an attribute that isn’t valid in the XHTML 1.0 specification.
Now, if you publish your website (which you should be doing in order to put it on a production/public server) the markup generated will be W3C-compliant and will validate properly. Obviously, this assumes that the markup YOU add to your pages complies with W3C recommendations and standards.
Awesome! Solved a big problem for me, thanks!