locked
Get client browser version in C# (When IE Compatibility mode enabled) RRS feed

  • Question

  • User-721741165 posted

    We need to know client's browser version from C# code (Server side). We use Request.Browser or HTTP_USER_AGENT to get the details. However, In IE when compatibility mode is enabled it always returns IE 7 version irrespective of version of IE. ie, even in IE 11 when compatibility mode enabled it returns IE 7 but I want actual version which is IE 11. Is there any way I can get actual IE version?

    Monday, March 2, 2015 11:23 PM

Answers

  • User281315223 posted

    Generally, you should always avoid "browser detection" and instead use "feature" detection to determine which functionality you want to allow / disallow based on the capabilities of the browser. There are various approaches to handling this but if you want to handle it on the server-side of things (C#), you are going to be limited to what the actual client passes back (like the User Agent).

    The way that the actual "mode" or "version" of Internet Explorer is handled is going to be within the user agent that is present. You can check for various properties and their values similar to this Javascript approach :

    // Check the browser version with the rest of the agent string to detect compatibility mode
    if (ua.indexOf("Trident/7.0") > -1 {
                this.version = 11;                      // IE 11
    }
    else if (ua.indexOf("Trident/6.0") > -1) {
                if (ua.indexOf("MSIE 7.0") > -1) {
                    this.compatibilityMode = true;
                    this.version = 10;                  // IE 10
                }
    }
    else if (ua.indexOf("Trident/5.0") > -1) {      
                if (ua.indexOf("MSIE 7.0") > -1) {
                    this.compatibilityMode = true;
                    this.version = 9;                   // IE 9
                }
    }
    else if (ua.indexOf("Trident/4.0") > -1) {
                if (ua.indexOf("MSIE 7.0") > -1) {
                    this.compatibilityMode = true;
                    this.version = 8;                   // IE 8
                }
    }
    else if (ua.indexOf("MSIE 7.0") > -1)
                this.version = 7;                       // IE 7
    else
                this.version = 6;                       // IE 6

    ua in this context is going to be the user agent string.

    Is there any particular reason that you are checking on the server-side within C#?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 3, 2015 8:48 AM
  • User61956409 posted

    Hi RamaprasadUpadhyaya,

    Thanks for your post.

    Firstly, you could try Rion’s solution on your side.

    Is there any particular reason that you are checking on the server-side within C#?

    Besides, you could refer to this article to detect the browser on the clients side.

    Best Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 3, 2015 9:03 PM
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 3, 2015 11:26 PM

All replies

  • User281315223 posted

    Generally, you should always avoid "browser detection" and instead use "feature" detection to determine which functionality you want to allow / disallow based on the capabilities of the browser. There are various approaches to handling this but if you want to handle it on the server-side of things (C#), you are going to be limited to what the actual client passes back (like the User Agent).

    The way that the actual "mode" or "version" of Internet Explorer is handled is going to be within the user agent that is present. You can check for various properties and their values similar to this Javascript approach :

    // Check the browser version with the rest of the agent string to detect compatibility mode
    if (ua.indexOf("Trident/7.0") > -1 {
                this.version = 11;                      // IE 11
    }
    else if (ua.indexOf("Trident/6.0") > -1) {
                if (ua.indexOf("MSIE 7.0") > -1) {
                    this.compatibilityMode = true;
                    this.version = 10;                  // IE 10
                }
    }
    else if (ua.indexOf("Trident/5.0") > -1) {      
                if (ua.indexOf("MSIE 7.0") > -1) {
                    this.compatibilityMode = true;
                    this.version = 9;                   // IE 9
                }
    }
    else if (ua.indexOf("Trident/4.0") > -1) {
                if (ua.indexOf("MSIE 7.0") > -1) {
                    this.compatibilityMode = true;
                    this.version = 8;                   // IE 8
                }
    }
    else if (ua.indexOf("MSIE 7.0") > -1)
                this.version = 7;                       // IE 7
    else
                this.version = 6;                       // IE 6

    ua in this context is going to be the user agent string.

    Is there any particular reason that you are checking on the server-side within C#?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 3, 2015 8:48 AM
  • User61956409 posted

    Hi RamaprasadUpadhyaya,

    Thanks for your post.

    Firstly, you could try Rion’s solution on your side.

    Is there any particular reason that you are checking on the server-side within C#?

    Besides, you could refer to this article to detect the browser on the clients side.

    Best Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 3, 2015 9:03 PM
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 3, 2015 11:26 PM