开发者

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

I want to dete开发者_如何转开发rmine the browser type in code-behind file using C# on ASP.NET page.

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

How can I determine the browser type?


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

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


You may also want to look at: 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:

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.


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


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

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜