Send Out XHTML to Browsers That Support It

A while ago I saw this useful tip to instruct an ASP.NET 2.0 application to send the response as valid XHTML / XML.
It's as simple as adding the following to your Global class / global.asax file:
Sub Application_PreSendRequestHeaders(ByVal o As Object, _
        ByVal e As EventArgs)
If Array.IndexOf(Request.AcceptTypes, "application/xhtml+xml") > -1 Then Response.ContentType = "application/xhtml+xml" End If End Sub C# protected void Application_PreSendRequestHeaders(Object o, EventArgs e) { if (Array.IndexOf(Request.AcceptTypes, "application/xhtml+xml") > -1) { Response.ContentType = "application/xhtml+xml"; } }

This way, browsers that support it (like FireFox) get the page served as XHTML / XML, while other browsers (like IE), get it as plain HTML.

You can read the full details in the XHTML and MIME Types section of the article "Building ASP.NET 2.0 Web Sites Using Web Standards" at the MSDN site.

Where to Next?

Wonder where to go next? You can read existing comments below or you can post a comment yourself on this article .

Consider making a donation
Please consider making a donation using PayPal. Your donation helps me to pay the bills so I can keep running Imar.Spaanjaars.Com, providing fresh content as often as possible.

Feedback by Other Visitors of Imar.Spaanjaars.Com

On Tuesday, June 13, 2006 11:40:12 AM Jørn Schou-Rode said:
You should add the Vary header to the output as well, when doing content negotiation: Response.AddHeader("Vary", "Accept");
On Sunday, July 9, 2006 12:21:07 PM Ian Jones said:
I previously read this msdn article and have just got round to trying it out.

One thing obviously missing is that this assumes all content served will want to use the "application/xhtml+xml" tag.
Where as if you wanted to serve a graphic, pdf, word document etc you would want to use a different tag.

So here below is my implementation.
First I check for nulls on accept types, particularly usefull for when reading an rss feed from .net which apparently doesn't send any accept types.
Then I placed a filter to only look at .aspx pages, you could change this to look at html pages as well.

Then I check to see what the current content type is, as the aspx page maybe returning a pdf, word document from a CMS.

And Then if the browser supports it to set the content type to "application/xhtml+xml";

and add the Response.AddHeader("Vary", "Accept"); as suggested above, thanks.

    protected void Application_PreSendRequestHeaders(Object o, EventArgs e)
        if (Request.AcceptTypes != null)
            String MyRequest = Request.Url.ToString();
            if (MyRequest.EndsWith(".aspx"))
                if (Response.ContentType == "text/html")
                    if (Array.IndexOf(Request.AcceptTypes, "application/xhtml+xml") > -1)
                        Response.ContentType = "application/xhtml+xml";
                        Response.AddHeader("Vary", "Accept");

I have just written this quickly so feel free to comment and improve on this code.
On Friday, July 14, 2006 6:12:53 PM Imar Spaanjaars said:
Hi Jørn and Ian,

Thank you both for your additios. Very useful.


On Wednesday, February 14, 2007 2:17:58 PM Bachu said:
Hi, my site is in xhtml1.1 & 1.0, I have all my pages valid according to the w3 standards, but why is that itself has few invalid pages, over 10 pages.
On Wednesday, February 14, 2007 2:29:29 PM Imar Spaanjaars said:
Hi Bachu,

Not sure why you're asking me.... Why don't you ask W3C directly?

On Friday, February 8, 2008 2:33:29 PM Gary Nelson said:
Can you tell me the benefit(s) of "Send Out XHTML to Browsers That Support It"?

Talk Back! Comment on Imar.Spaanjaars.Com

I am interested in what you have to say about this article. Feel free to post any comments, remarks or questions you may have about this article. The Talk Back feature is not meant for technical questions that are not directly related to this article. So, a post like "Hey, can you tell me how I can upload files to a MySQL database in PHP?" is likely to be removed. Also spam and unrealistic job offers will be deleted immediately.

When you post a comment, you have to provide your name and the comment. Your e-mail address is optional and you only need to provide it if you want me to contact you. It will not be displayed along with your comment. I got sick and tired of the comment spam I was receiving, so I have protected this page with a simple calculation exercise. This means that if you want to leave a comment, you'll need to complete the calculation before you hit the Post Comment button.

If you want to object to a comment made by another visitor, be sure to contact me and I'll look into it ASAP. Don't forget to mention the page link, or the QuickDocId of the document.

For more information about the Talk Back feature, check out this news item.