如何使用ASP从服务器端确定浏览器类型。净& # ?

时间:2021-10-20 16:15:16

I want to determine the browser type in code-behind file using C# on ASP.NET page.

我想使用ASP上的c#来确定代码隐藏文件中的浏览器类型。网络页面。

If it is IE 6.0, I have to execute certain lines of code.

如果是IE 6.0,我必须执行某些代码行。

How can I determine the browser type?

如何确定浏览器类型?

4 个解决方案

#1


35  

You can use Request.Browser to identify the browser info. These MSDN 1 & 2 article gives more info abt this.

您可以使用请求。浏览器识别浏览器信息。这些MSDN 1和2文章提供了更多的信息。

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";

#2


10  

You may also want to look at: Request.ServerVariables.

您可能还需要查看:Request.ServerVariables。

I have used:

我用:

string UserAgent = Request.ServerVariables["HTTP_USER_AGENT"];
Response.Write("User: " + UserAgent);
if(UserAgent.Contains("MSIE")) {
   //do something
} 

to show me what browser is being used. This can give you a response for IE similar to:

向我展示正在使用的浏览器。这可以给你一个类似的回应:

User: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; Trident/4.0; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.5.21022; Tablet PC 2.0; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET CLR 1.1.4322)

Depending on your version of IE or other browser. Firefox gives me:

取决于你的IE版本或其他浏览器。火狐给我:

User: Mozilla/5.0 (Windows NT 6.0; rv:11.0) Gecko/20100101 Firefox/11.0

It is important to note: I would use the ServerVariables over the Browser Capabilities because using BrowserCapabilities on Chrome will currently return "Desktop" which seems to be the same for Safari when I check it on a mac.

需要注意的是:我将在浏览器功能上使用服务器变量,因为在Chrome上使用浏览器功能将会返回“桌面”,当我在mac上检查它时,它看起来和Safari是一样的。

#3


1  

This should list all browser capabilities...

这应该列出所有浏览器的功能……

System.Web.HttpBrowserCapabilities browser = Request.Browser;
IDictionaryEnumerator enumerator = browser.Capabilities.GetEnumerator();
while (enumerator.MoveNext())
{
    string key = (string)enumerator.Key.ToString();
    object value = enumerator.Value;
    Response.Write(String.Format("Key = {0}, Value = {1}", key, value));
}

#4


0  

Since Request is a property of the page class, the above code gave me the error Request does not exist and I used the following code to get the browser type

由于Request是page类的属性,因此上面的代码给出了错误请求不存在,我使用了以下代码来获取浏览器类型

private string GetBrowserType()
{
    string browserType = string.Empty;
    if (HttpContext.Current != null)
    {
        var request = HttpContext.Current.Request;
        browserType = request.Browser.Type;
    }
    return browserType;
}

It gave me Chrome64 with Chrome and InternetExplorer11 with IE

Chrome给了我Chrome64, IE给了我InternetExplorer11

#1


35  

You can use Request.Browser to identify the browser info. These MSDN 1 & 2 article gives more info abt this.

您可以使用请求。浏览器识别浏览器信息。这些MSDN 1和2文章提供了更多的信息。

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";

#2


10  

You may also want to look at: Request.ServerVariables.

您可能还需要查看:Request.ServerVariables。

I have used:

我用:

string UserAgent = Request.ServerVariables["HTTP_USER_AGENT"];
Response.Write("User: " + UserAgent);
if(UserAgent.Contains("MSIE")) {
   //do something
} 

to show me what browser is being used. This can give you a response for IE similar to:

向我展示正在使用的浏览器。这可以给你一个类似的回应:

User: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; Trident/4.0; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.5.21022; Tablet PC 2.0; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET CLR 1.1.4322)

Depending on your version of IE or other browser. Firefox gives me:

取决于你的IE版本或其他浏览器。火狐给我:

User: Mozilla/5.0 (Windows NT 6.0; rv:11.0) Gecko/20100101 Firefox/11.0

It is important to note: I would use the ServerVariables over the Browser Capabilities because using BrowserCapabilities on Chrome will currently return "Desktop" which seems to be the same for Safari when I check it on a mac.

需要注意的是:我将在浏览器功能上使用服务器变量,因为在Chrome上使用浏览器功能将会返回“桌面”,当我在mac上检查它时,它看起来和Safari是一样的。

#3


1  

This should list all browser capabilities...

这应该列出所有浏览器的功能……

System.Web.HttpBrowserCapabilities browser = Request.Browser;
IDictionaryEnumerator enumerator = browser.Capabilities.GetEnumerator();
while (enumerator.MoveNext())
{
    string key = (string)enumerator.Key.ToString();
    object value = enumerator.Value;
    Response.Write(String.Format("Key = {0}, Value = {1}", key, value));
}

#4


0  

Since Request is a property of the page class, the above code gave me the error Request does not exist and I used the following code to get the browser type

由于Request是page类的属性,因此上面的代码给出了错误请求不存在,我使用了以下代码来获取浏览器类型

private string GetBrowserType()
{
    string browserType = string.Empty;
    if (HttpContext.Current != null)
    {
        var request = HttpContext.Current.Request;
        browserType = request.Browser.Type;
    }
    return browserType;
}

It gave me Chrome64 with Chrome and InternetExplorer11 with IE

Chrome给了我Chrome64, IE给了我InternetExplorer11