Quirks mode refers to a technique used by some web browsers for the sake of maintaining backward compatibility with web pages designed for older browsers, instead of strictly complying with W3C and IETF standards in standards mode. Here’s how to force IE into submission by adding a DLL.

The problem

Sometimes the browser decides to go into quirks mode. The X-UA-Compatible header can be used to instruct IE to load a specific version. But what if the user is debugging and asks for IE9? The browser changes it query string, but will be forced back if we just add the IE=edge. That’s why I¬†created a module that inspects the Trident token and adds the right header.

The code

So let’s take a look at the code:

using System;
using System.Configuration;
using System.Web;

namespace KeesTalksTech.WebUI.ExtendedModules
{
	/// <summary>
	/// Module that will detect an IE browser and add the right X-UA-Compatible header.
	/// </summary>
	public class FixQuirksModule : IHttpModule
	{
		private static bool? useEdge;

		/// <summary>
		/// Gets a value indicating whether to use an edge header.
		/// </summary>
		/// <value>
		///   <c>true</c> if edge should be used; otherwise, <c>false</c>.
		/// </value>
		private static bool UseEdge
		{
			get
			{
				if (useEdge == null)
				{
					bool result = false;
					Boolean.TryParse(ConfigurationManager.AppSettings["FixQuirksModule.UseEdge"], out result);
					useEdge = result;
				}

				return useEdge.GetValueOrDefault();
			}
		}

		/// <summary>
		/// Disposes of the resources (other than memory) used by the module that implements 
		/// <see cref="T:System.Web.IHttpModule"/>.
		/// </summary>
		public void Dispose()
		{
		}

		/// <summary>
		/// Initializes a module and prepares it to handle requests.
		/// </summary>
		/// <param name="context">An <see cref="T:System.Web.HttpApplication"/> that provides access 
		/// to the methods, properties, and events common to all application objects within an 
		/// ASP.NET application</param>
		public void Init(HttpApplication context)
		{
			context.BeginRequest += new EventHandler(AddResponseHeader);
		}

		/// <summary>
		/// Adds the response header.
		/// </summary>
		/// <param name="sender">The sender.</param>
		/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
		private void AddResponseHeader(object sender, EventArgs e)
		{
			HttpApplication application = sender as HttpApplication;
			var browser = application.Request.Browser;

			if (browser.Browser == "IE")
			{
				if (application.Response.Headers["X-UA-Compatible"] == null)
				{
					if (UseEdge)
					{
						application.Response.Headers.Add("X-UA-Compatible", "IE=edge");
					}
					else if (browser.MajorVersion > 7)
					{
						application.Response.Headers.Add("X-UA-Compatible", "IE=EmulateIE" + browser.MajorVersion);
					}
					else if (browser.MajorVersion == 7)
					{
						string userAgent = application.Request.UserAgent;

						if (userAgent.Contains("Trident/7.0"))
						{
							application.Response.Headers.Add("X-UA-Compatible", "IE=EmulateIE11");
						}
						else if (userAgent.Contains("Trident/6.0"))
						{
							application.Response.Headers.Add("X-UA-Compatible", "IE=EmulateIE10");
						}
						else if (userAgent.Contains("Trident/5.0"))
						{
							application.Response.Headers.Add("X-UA-Compatible", "IE=EmulateIE9");
						}
						else if (userAgent.Contains("Trident/4.0"))
						{
							application.Response.Headers.Add("X-UA-Compatible", "IE=EmulateIE8");
						}
						else
						{
							application.Response.Headers.Add("X-UA-Compatible", "IE=EmulateIE7");
						}
					}
				}
			}
		}
	}
}

Compile it into a DLL and add the following to the web.config:

<system.webServer>
	<modules>
		<remove name="FixQuirksMode" />
		<add name="FixQuirksMode" 
		     type="KeesTalksTech.WebUI.ExtendedModules.FixQuirksModule, WebUI.ExtendedModules"/>
	</modules>
</system.webServer>

Download

You can download the DLL from GitHub.

That’s it. Hope it helps.