如何使用 ASP.NET & C# 从服务器端确定浏览器类型?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2311077/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 01:18:23  来源:igfitidea点击:

How to determine Browser type from Server side using ASP.NET & C#?

c#asp.net

提问by Rita

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

我想在 ASP.NET 页面上使用 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?

如何确定浏览器类型?

采纳答案by RameshVel

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

您可以使用 Request.Browser 来识别浏览器信息。这些 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";

回答by CaptainBli

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:

向我展示正在使用的浏览器。这可以为您提供类似于以下内容的 IE 响应:

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 或其他浏览器的版本。Firefox 给了我:

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.

重要的是要注意:我会在浏览器功能上使用 ServerVariables,因为在 Chrome 上使用 BrowserCapabilities 当前将返回“桌面”,当我在 mac 上检查它时,它似乎与 Safari 相同。

回答by cymorg

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));
}

回答by Raj Baral

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

由于Request是页面类的一个属性,上面的代码给了我错误请求不存在,我用下面的代码来获取浏览器类型

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 Chrome64with Chrome and InternetExplorer11with IE

它给了我Chrome64Chrome 和InternetExplorer11IE