Justin Toth's Blog

Justin is a web developer living in Maryland

Making your ASP.NET web forms app valid with XHTML 1.0 Strict

clock March 19, 2010 13:01 by author Justin Toth

Today I was working on making SportsAlert valid against the doctype XHTML 1.0 Strict. I used http://validator.w3.org to test my homepage and fixed all of the issues except for 2. There are 2 issues inherent in ASP.NET web forms. One is that it adds name="aspnetForm" to the <form> tag, which is valid in XHTML Transitional but not XHTML Strict. Secondly, it adds input tags for things such as Viewstate right after the opening <form> tag, which is also valid in XHTML Transitional but not in XHTML Strict. To fix these 2 issues, you need to include:

<xhtmlConformance mode="Strict" />

In your web.config file within the system.web node. This will remove the name="aspnetForm" attribute from the <form> tag and will place the input tags within a div tag so that they have a valid container.

That should be enough, however for some reason the w3.org validator will still complain about these two things and when you look at the source code that it's pulling down, it's different from the real source code. The name="aspnetForm" will still be on the form tag and the input tags won't be wrapped in the div tag, when you can clearly see by doing view source on the page that those changes have been made. The solution is to add a .browser file to the App_Browsers directory in your ASP.NET application:


<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>

 

I didn't take the time to figure out why this works but it does and my page is now XHTML 1.0 Strict valid, so I'm happy... :)



Introduction

clock May 3, 2009 19:30 by author Justin Toth

This is my first post and I'm excited to start blogging!! It seems like every day I run into some sort of issue and once I figure out how to solve it, it makes me wish I had a blog so I could post the solution so that others won't have to spend as much time on it as I did.

This blog will be related to .NET development, and more specifically front-end web development. I used to consider myself an ASP.NET developer but I feel like we've come upon a crossroads. Straight ahead is ASP.NET, to the left is pure javascript development, and to the right is Silverlight. You look further down the ASP.NET road and see that there's another branch up beyond this one, ASP.NET going to the left and ASP.NET MVC going to the right. Which road to take??



About the author

Justin

Justin is a senior developer who has been working with .NET since 2003. His main focus is building highly-interactive web applications using ASP.NET MVC and Dojo or jQuery. Visit his company's site at http://tothsolutions.com.

Page List

Sign in