2014-09-10 49 views
2

如果用戶的瀏覽器兼容性打開,那麼需要向用戶顯示您的瀏覽器的兼容性信息。如何知道使用asp.net是否打開或關閉兼容性(c#)

我在谷歌上搜索了很多,但還沒有找到合適的答案。

我曾嘗試下面的代碼,但HttpContext.Current.Request.UserAgent始終包含MSIE 7.0

string isOn = string.Empty; 

if (HttpContext.Current.Request.UserAgent.IndexOf("MSIE 7.0") > -1) 
{ 
isOn = "IE8 Compatibility View";` 

    } 
    else 
    { 
    isOn = "IE8"; 
    } 

} 
+0

的可能重複的[區分在IE7兼容模式IE7瀏覽器和瀏覽器(http://stackoverflow.com/questions/10213639/differentiate-ie7-browser-and-browser-in-ie7-compatibility - 模式) – Curt 2014-09-10 08:45:10

+0

有什麼方法可以找到兼容性是否在 – user2078643 2014-09-10 09:01:02

回答

0

你可以試試這樣

if (Request.Browser.Type.ToUpper().Contains("IE")) 
{ 
    if (Request.Browser.MajorVersion < 7) 
    { 
     //Show the message here 
    } 
    ... 
} 
else if (Request.Browser.Type.Contains("Firefox")) 
{ 
    //code to show message 
} 
else if (Request.Browser.Type.Contains("Chrome")) 
{ 
    //code to show message 
} 

還要檢查這個MSDN有它自己的檢測瀏覽器的方式

查詢瀏覽器屬性,其中包含一個HttpBrowserCapabilities 對象。此對象在HTTP請求期間從瀏覽器或客戶端設備 獲取信息,告訴您的應用程序支持瀏覽器或客戶端設備提供的類型和級別。該對象依次使用強類型 屬性和通用名稱 - 值字典公開關於瀏覽器功能的信息。

private void Button1_Click(object sender, System.EventArgs e) 
{ 
    System.Web.HttpBrowserCapabilities browser = Request.Browser; 
    string s = "Browser Capabilities\n" 
     + "Type = "     + browser.Type + "\n" 
     + "Name = "     + browser.Browser + "\n" 
     + "Version = "     + browser.Version + "\n" 
     + "Major Version = "   + browser.MajorVersion + "\n" 
     + "Minor Version = "   + browser.MinorVersion + "\n" 
     + "Platform = "    + browser.Platform + "\n" 
     + "Is Beta = "     + browser.Beta + "\n" 
     + "Is Crawler = "    + browser.Crawler + "\n" 
     + "Is AOL = "     + browser.AOL + "\n" 
     + "Is Win16 = "    + browser.Win16 + "\n" 
     + "Is Win32 = "    + browser.Win32 + "\n" 
     + "Supports Frames = "   + browser.Frames + "\n" 
     + "Supports Tables = "   + browser.Tables + "\n" 
     + "Supports Cookies = "  + browser.Cookies + "\n" 
     + "Supports VBScript = "  + browser.VBScript + "\n" 
     + "Supports JavaScript = "  + 
      browser.EcmaScriptVersion.ToString() + "\n" 
     + "Supports Java Applets = " + browser.JavaApplets + "\n" 
     + "Supports ActiveX Controls = " + browser.ActiveXControls 
       + "\n" 
     + "Supports JavaScript Version = " + 
      browser["JavaScriptVersion"] + "\n"; 

    TextBox1.Text = s; 
} 
相關問題