asp.net and the W3C validator
Posted in .Net-Development by Matthew Clements on 30 Jul, 2009
Validation of XHTML and CSS is of growing importance for both cross-browser compatibility of code as well as accessibility and semantic understanding of your website.
ASP.Net scuppers these attempts by needing 2 pieces of configuration in order to make it valid AND allow the W3C validator to recognise the fact
Firstly the following lines are needed in your web.config:
<system.web>
<!-- other elements here -->
<xhtmlConformance
mode="Legacy" />
</system.web>
Available options are "Legacy", "Transitional" and "Strict": take your pick..
Adding this to your web.config WILL make the output rendered to the browser XHTML compliant - however the level of this is goverened by how the browser identifies itself to the IIS web server, with 'older' or 'unrecognised' browsers it doesn't bother to transform the output.
Unfortunately the W3 validator (http://validator.w3.org/) isn't recoginsed by IIS as an XHTML compatible browser so it outputs at best Transitional content.
To fix this add an "App_Browsers" folder to your solution ("Add ASP.Net Folder" from the contect menu on the project in VS.Net) and create a file called "WhateverYouLike.browser" with the following content:
<browsers>
<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>
deploy this to your server and you'll have a much better chance of achieving XHTML Strict code through .Net
Hope it helps!
3 Responses to "asp.net and the W3C validator"
- Houston Nightclubs Says:
- on 17 Sep, 2009
- I tried that, but what if i want to validate it as HMTL 4.01 Strict? I changed the web.config to strict and it still doesn't like the viewstate ID that starts with _ Any thoughts?
- Kris Says:
- on 29 Mar, 2010
- That helps a lot! Thank you for posting this.
- w3cvalidation Says:
- on 16 Apr, 2010
- Nice information, I really appreciate the way you presented.Thanks for sharing..

