Click here to Skip to main content
15,881,866 members
Articles / Web Development / ASP.NET

W3C_Validator with ASP.NET or how to validate XHTML 1.1

Rate me:
Please Sign up or sign in to vote.
4.00/5 (1 vote)
25 Nov 2008CPOL2 min read 31.3K   127   17  
This article is from a public bug list of W3C, describing errors in XHTML 1.1 validation of ASP.NET pages.

Introduction

This is all about standards. When you want to write a new web browser, you need to start from the start. But what is start? Standard, of course. Your new web browser has to implement a standard or a couple of standards. You probably have heard about HTML, it comes from 'hyper text markup language', the first standard for web pages. So, when your application validates to a standard and the web browsers have implemented the same standard, you may be sure your page will look the same on each of them.

XHTML is a new generation standard, the next step from HTML, that on its mission holds to separate program code from presentation level. According to it, you have to place all presentation markups into style sheets. More about XHTML 1.1 can be read here. Introduces in 2001, so not so new, but even today, not many websites can be validated against this.

Making your website valid against XHTML1.1 not only shows you as someone who understands the problem and strength of standardization, it also makes your web application be more manageable and teaches you to make it more logically constructed.

Let's make W3C-validator happy

The best way to start with validation check is the source, of course: http://validator.w3.org/.

Step 1: Adding Valid DOCTYPE and HTML Tags

Add this to each .aspx page or just to the .master page:

ASP.NET
<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
    "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">

Step 2: Bringing ASP.NET Application into 'Strict' Mode

This is the most simple action. Just add this line to the web.config file:

XML
<system.web>
      <xhtmlConformance mode="Strict" />

Step 3: Cleaning Markup

Make all markups according to the XHTML standard.

Step 4: Going crazy... joke

Every step with ASP.NET is done, but these two errors stay:

Image 1

What do we do? Yep, this question can make you mad, 'cause you have cleaned your code, and all markups are clean'.

When you view the source code of the response, you will see:

ASP.NET
<body>
    <form method="post" action="default.aspx" id="aspnetForm">
<div>
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE"
value="/wEPDwULLTE3OTA2NzE4NzhkZNfmNwEXXdZH1+AqEfOHXBehT/kY" />
</div>

which is valid against XHTML 1.1, but when you check with the W3C validator, it shows those errors and the source code will look like this:

ASP.NET
<body>
<form name="aspnetForm" method="post" 
   action="default.aspx" id="aspnetForm">
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE"
value="/wEPDwULLTE3OTA2NzE4NzhkZNfmNwEXXdZH1+AqEfOHXBehT/kY" />

Where is the problem? ASP.NET and .NET Framework v2.0 (v3.0, v3.5) do not include the Validation Service engine in their recognized browsers and the rendered code gets 'down-leveled' when rendered. This produces the errors in code that are sent to the Validation Service engine thus causing the code not to validate. (Craig Thacker comments at W3C bugzilla).

So, as a workaround, you have to assign the W3C validator client by yourself.

Step 4: Assign W3C Validator Capabilities

Create the App_Browsers folder in the web root directory, and a file with .browser extension. It must contain this code:

ASP.NET
<!-- App_Browsers/w3g.browser
http://www.w3.org/Bugs/Public/show_bug.cgi?id=3734
-->
<browsers>
    <browser id="W3C_Validator" parentID="default">
        <identification>
            <userAgent match="^W3C_Validator" />
        </identification>
<!--
        <capture>
            <userAgent match="NewBrowser (?'version'\d+\.\d+)" />
        </capture>
 -->
        <capabilities>
          <capability name="browser"              value="W3C Validator" />
          <capability name="ecmaScriptVersion"    value="1.2" />
          <capability name="javascript"           value="true" />
          <capability name="supportsCss"          value="true" />
          <capability name="tables"               value="true" />
          <capability name="tagWriter" value="System.Web.UI.HtmlTextWriter" />
          <capability name="w3cdomversion"        value="1.0" />
        </capabilities>
    </browser>
  <!--
    <browser refID="Mozilla">
        <capabilities>
            <capability name="xml" value="true" />
        </capabilities>
    </browser>
     -->
</browsers>

Now, the validator will show in green: 'This document was successfully checked as XHTML 1.1!'

History

  • 22.11.2008 - Rewritten according to comments.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Ukraine Ukraine
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --