4

I have Getaway in my MVC3 application in Layout :

@if ((Request.Browser.Browser == "IE") && ((Request.Browser.MajorVersion == 7)))
{
//show some content
}
else
{
//show another content 
}

I have many users complains (users with internet explorer 8). They see Internet explorer 7 content from my app. What wrong with my way of detecting Internet explorer 7 version? How can I be sure for 100% in my application that user have internet explorer 7 version? May be this is specific OS problem?

Denys Denysenko
  • 7,598
  • 1
  • 20
  • 30
freethinker
  • 2,257
  • 4
  • 26
  • 49

2 Answers2

14

The issue is the HttpBrowserCapabilities aka Request.Browser class parses the userAgent header from the request which has information about the client (in your case the browser) which might not always be 100% reliable as user agents are easily changeable.

If you know what value MajorVersion is returning and it's consistent enough you could possibly put a fix in for it. Alternatively you could try checking for browsers lower than IE8 instead (again though, not 100%) e.g.

@if ((Request.Browser.Browser == "IE") && ((Request.Browser.MajorVersion < 8)))
{
    //show IE7 content
}
else
{
    //show IE8+ content 
}
James
  • 80,725
  • 18
  • 167
  • 237
  • Hi. Thanks for reply. I already changed to Request.Browser.MajorVersion < 8, but this is not help. After many tests i found that problem is appear on Windows XP with ie8. Request.Browser.MajorVersion returns 7 – freethinker Dec 12 '12 at 10:11
  • You could parse out the OS version and if it's Windows XP change your check to `Request.Browser.MajorVersion < 7` for IE7 otherwise `Request.Browser.MajorVersion == 7` – James Dec 12 '12 at 10:28
  • OS version? This is web application... It is possible? – freethinker Dec 12 '12 at 10:42
  • @user1086344 yes, the OS version is generally sent along as part of the user agent e.g. `Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)` - In this scenario the browser is IE9 and the OS is [Windows 7 (or Windows Server 2008)](http://en.wikipedia.org/wiki/Windows_NT_6.1). For XP you would be looking for `NT 5.X`. – James Dec 12 '12 at 11:00
  • For internet explorer 11, Browser equals to "InternetExplorer".. You may want to update the answer. – yakya Feb 12 '17 at 10:18
  • @sotn whilst that may be true (can you clarify with docs?) the answer is tailored for compatibility with IE7 and below so still stands. Tbh all that proves is what I've already mentioned in that these types of checks are unreliable. – James Feb 15 '17 at 12:56
  • @James You're right, the answer still holds. I overlooked your answer sorry :) – yakya Feb 15 '17 at 13:51
3

The version number needs to be specific for IE.

 if (Request.Browser.Browser == "IE" && Request.Browser.Version == "7.0") 
        { 
          //Show IE 7 content
        }
 else
  {
     // Show other than IE7 content
  }
RGR
  • 1,521
  • 2
  • 22
  • 36