如何使用asp.net(c)了解兼容性是打开还是关闭

本文关键字:兼容性 了解 asp 何使用 net | 更新日期: 2023-09-27 18:21:15

如果用户的浏览器兼容性打开,我必须向用户显示您的浏览器兼容性已打开的消息。

我在谷歌上搜索了很多,但还没有找到合适的答案。

我尝试了以下代码,但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";
    }
}

如何使用asp.net(c)了解兼容性是打开还是关闭

你可以试试这个

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,它有自己的检测浏览器的方法

查询Browser属性,该属性包含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;
}