Loading ...

XHTML Rendering in ASP.NET Web Sites | CodeAsp.Net

XHTML Rendering in ASP.NET Web Sites

 /5
0 (0votes)

First of all, in this post, I'm going to show you how to configure XHTML Rendering in ASP.NET Web Sites.

Ok so...if you've been reading any of my previous posts or emails, you may know that there are times where I come up against a programming problem that I just cant seem to fix...and I tend to be famous for "banging my head against the wall" until a solution pops out. Just so you know...I don't really bang my head against the wall, it's just an expression I use when I'm find a solutions to an issue that I never came across before.

Well, here is another post that comes from "banging my head against the wall."

I was dynamically generating the META DESCRIPTION and KEYWORD tags from a base class that we use for all pages on the website. I'd upload them to the live production website, open up the WC3 validator and BAM!... invalid meta tags.

I wanted to make sure that the page I created "validates" and uses proper HTML syntax. There are many benefits to having your pages validate...here are a few that I lifted from Microsoft:

  • It guarantees that the elements in the pages are well formed.
  • Because many browsers are increasingly moving toward supporting XHTML, creating pages that conform to XHTML standards helps ensure that your pages render consistently in all browsers.
  • Using XHTML helps to make pages conform more readily to accessibility standards.
  • XHTML is extensible, allowing the definition of new elements.
  • An XHTML page is much easier to read programmatically for situations in which the Web page is processed by a computer instead of being read by users, and the document can be manipulated using transformations.
  • ...and it provides all kinds of advantages for SEO for your website.

So, as you can see above, being able to configure XHTML Rendering in ASP.NET Web Sites is very important.

Now...the error message that the validator was giving me: "You may have neglected to close an element, or perhaps you meant to "self-close" an element, that is, ending it with "/>" instead of ">"."

To better describe this, I started with:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

And after ASP.NET got a hold of it, I ended up with:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

Yuk. As you can see, it removed the closing tag.

At this point, I was cursing ASP.NET and yes, banging my head against the wall. Once again. Ha.

So I did a bit of digging on the net, and I found this page:

http://msdn.microsoft.com/en-us/library/exc57y7e.aspx

...which is interesting (and it's were I lifted the benefits of conforming XHTML pages)...and it basically says that yes, ASP.NET and XHTML works together pretty well these days.

So I was thinking, ...if they work well together, then why am I having these issues with the malformed META Tags? The problem was, that I had a runat="server" in the head tag, so every time the page renders ASP.NET wants to reformat my meta tags, removing the closing tag ( /> ).

And it was on this page where I found the solution:

http://msdn.microsoft.com/en-us/library/ms178159.aspx

It turns out that by default, ASP.NET web applications are configured to use a "legacy" setting, which is similar to how markup was rendered in previous versions of ASP.NET...and we all know that in the early days, ASP.NET was not very XHTML friendly.

This setting in configured in the web.config file and looks like this:

<system.web>
    <xhtmlConformance mode="Legacy" />
</system.web>

So, what I did, was swap out the "Legacy" with "Transitional" and wouldn't you know...my problem was solved.

I hope you can get a good "take-away" from this post.

Many thanks.

And again, if you have anything to add...please comment below.

Comments (1)

   
Anonymous
Thanks for posting this - its exactly the problem I was having.
7/31/2009
 · 
by
  • :*
  • :*
  • :
 *

Top Posts