Good code is its own best documentation. -- Steve McConnell
Welcome to my blog and project site for Microsoft.NET development.

I've been a full time .NET developer for ten years, but I didn't start my professional life as a programmer ... more
Share/Print this page:

What is Request.AcceptTypes in ASP.NET

A little known and little used property of the Request object

By steve on January 09, 2007.
Updated on January 22, 2012.
Viewed 5,318 times (0 times today).
Article TypesLanguagesTechnologiesTechnologiesTopics
OverviewC#ASP.NETWeb BrowsersWeb Design

Summary

Contents

There is a surprising lack of information about the Request.AcceptTypes property and what it's really for.

This property returns a string array of mime types supported by the client browser. For example, "text/html", "application/xml", etc. But what does this really mean? Let's do some experiments.

For the first experiment, we just see what the typical mime types are for a few different browsers. Add the following method to the code-behind of an aspx with a Label on the page called lblOutput.

Method to get and print the AcceptTypes array

Contents
private void TestRequestAcceptTypes()
{
   string[] types = Request.AcceptTypes;
   foreach (string type in types)
      lblOutput.Text += type + "<br>";
}

The following represent the page output from the above method for a few different browsers:

Accepted Mime types for Internet Explorer 6.0

Contents
*/*

Accepted Mime types for FireFox 1.0 and Netscape 8.0

Contents
text/xml
application/xml
application/xhtml+xml
text/html;q=0.9
text/plain;q=0.8
image/png
*/*;q=0.5

Accepted Mime types for Opera 8.5

Contents
text/html
application/xml;q=0.9
application/xhtml+xml
image/png
image/jpeg
image/gif
image/x-xbitmap
*/*;q=0.1

You will notice that for all of the above browsers, they each list / to indicate they accept all mime types. And Internet Explorer does not even explicitly list text/html! What's up with that? My version of Firefox supports embedded pdf files, so why isn't that listed among the mime types?

While the mime types may be useful for a few special cases, for example, if a browser says it supports application/xhtml then if you set that as the mime type of the delivered page, then it may have the ability to handle that page better than if the type were just text/html. I really don't know because I haven't encountered a case like this. One reason for that is because any good web developer should develop for all browsers. And besides, to make separate versions of a page for different browsers requires a lot of extra work. I simply use techniques and technologies which all standard browsers can handle nicely.

So my point is this, the published mime types from the client browser are virtually useless to the web developer, so you should ignore them as much as possible until there are consistent standards. That said, I suspect there are a few special cases out there somewhere.

Back to Top

User Comments (5)

Posted 2007 Mar 27 10:02 AM. reply
Hey,

I'm currently using AcceptTypes to create XHTML MP content for mobile devices where the range of browser capability creates more difficulties than Firefox/IE problems.

Pete
Reply 2007 Mar 27 21:04 PM by steve. reply
Cool. It's good to see that AcceptTypes is useful in some circumstances.
Posted 2008 Nov 22 06:30 AM. reply
Since all of my pages are XHTML I utilize AcceptTypes to determine if client browsers specifically support application/xhtml+xml. If so, I change Response.ContentType to "application/xhtml+xml". This invokes strict error checking in Firefox and a different DOM as well. If you want to serve your pages as true XHTML this is the way to do it.

// global.asax

void Application_PreSendRequestHeaders(Object sender, EventArgs e) {

// Serve Content-Type "application/xhtml+xml" to browsers that accept it

if (Request.AcceptTypes != null &&
Array.IndexOf(Request.AcceptTypes, "application/xhtml+xml") != -1 &&
Response.ContentType == "text/html" &&
Response.StatusCode == 200 &&
!Response.IsRequestBeingRedirected)
Response.ContentType = "application/xhtml+xml";
}

Kevin (SLORider.com)
Posted 2008 Nov 22 06:57 AM. reply
Here is a link to further information about using Request.AcceptTypes for "content negotiation" and serving XHTML:
http://idunno.org/archive/2005/02/01/229.aspx

Kevin (SLORider.com)
Posted 2009 Mar 12 18:56 PM. reply
I use the accept header to see if a request was the result of an img tag. The following code works except for some redirects in IE 7 (usually from the javascript links on google) that will return a false positive.

static bool IsImageRequest(HttpRequest request)
{
var acceptTypes = request.AcceptTypes;
if (acceptTypes == null)
return false; // BlackBerry doesn't send Accept

if (request.Browser.Browser == "IE")
{
if (acceptTypes.Length == 1) // IE 6 & 7 generally use Accept: */* for image requests
return true;
return false;
}

if (request.Browser.Browser == "Firefox")
{
return acceptTypes[0].StartsWith("image/"); // Firefox 2 & 3 use Accept: image/png,*/*;q=0.5 for image requests
}

if (acceptTypes.Length == 1) // Chrome uses Accept: */* for image requests
return true;
return false;
}

Bryan
Post Your Comment
  You may post without logging in or login here.
Display Name: Required.
Email: Required. Will not be shown. Used for identicon.
Comment:
Allowed tags: <quote></quote>, <code></code>, <b></b>, <i></i>, <u></u>, <red></red>
 
   Please type text as shown in the image at left.