Fix W3C validation failure on ImageButton control (Server-wise)

by Jesse 18. May 2009 19:55

Recently runs into a problem on passing validation my .NET pages on W3C validator(http://validator.w3.org). Generally speaking the HTML code rendered from ImageButton control contains a invalid html tag border="0" which caused failure on passing XHTML 1.0 Transitional check.

Microsoft claimed it is not their issue, as it is not appear in "View code" of IE, which sounds more like an excuse to me. Dig several pages in google, one guy in asp.net forume suggested to use app_browser file which is working for me. Following is my w3c.browser file which sits in ~/App_Browsers folder


<browsers>
    <browser id="W3C" 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>

However, there are many websites running on my server, it is not a enjoyable jobs to copy it to every website. A server-wise solution will be a big time-saver. Read the post about "Browser Definition File Schema" (http://msdn.microsoft.com/en-us/library/ms228122.aspx) carefully, I mean line by line and word by word. You may find following paragraph

However, if changes are made to .browser files in the %SystemRoot%\Microsoft.NET\Framework\version\CONFIG\Browsers directory, you must manually recompile the application by using the %SystemRoot%\Microsoft.NET\Framework\version\aspnet_regbrowsers.exe tool

Haha, this sounds like a server-wise solution. I copied my w3c.browser to both C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\CONFIG\Browsers and C:\WINDOWS\Microsoft.NET\Framework64\v2.0.50727\CONFIG\Browsers, if you cannot find the second folder, you may be running on a 32 bit system, so don't need to worry about it. And the last thing needs to do

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_regbrowsers.exe -i

The assembly will automatically installed into GAC. ^_^

Just out of curiosity, I reflected the code aspnet_regbrowsers generated. If you are a "dead cat" like me, here is the code

private bool W3cProcess(NameValueCollection headers, HttpBrowserCapabilities browserCaps)
{
    IDictionary capabilities = browserCaps.Capabilities;
    string target = browserCaps[string.Empty];
    RegexWorker worker = new RegexWorker(browserCaps);
    if (!worker.ProcessRegex(target, "^W3C_Validator"))
    {
        return false;
    }
    worker.ProcessRegex(browserCaps[string.Empty], @"^W3C_Validator/(?'version'(?'major'\d+)(?'minor'\.\d+)\w*).*");
    capabilities["browser"] = "w3cValidator";
    capabilities["majorversion"] = worker["${major}"];
    capabilities["minorversion"] = worker["${minor}"];
    capabilities["version"] = worker["${version}"];
    capabilities["w3cdomversion"] = "1.0";
    capabilities["xml"] = "true";
    capabilities["tagWriter"] = "System.Web.UI.HtmlTextWriter";
    browserCaps.AddBrowser("W3C");
    this.W3cProcessGateways(headers, browserCaps);
    bool ignoreApplicationBrowsers = false;
    this.W3cProcessBrowsers(ignoreApplicationBrowsers, headers, browserCaps);
    return true;
}

protected virtual void W3cProcessBrowsers(bool ignoreApplicationBrowsers, NameValueCollection headers, HttpBrowserCapabilities browserCaps)
{
}

protected virtual void W3cProcessGateways(NameValueCollection headers, HttpBrowserCapabilities browserCaps)
{
}

Powered by BlogEngine.NET 1.5.0.7
Theme by Mads Kristensen | Modified by Mooglegiant