Click here to Skip to main content
15,939,736 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to display browser with veriosn and device and OS but not displaying propr values with my code.
could you please correct me .


The below code is getting incorrect data.


I am using laptop with windows 10 and browser chrome.

so result should be display with Chrome and lap and windows 10

But my result is displaying like below
OS: Windows 10 ~ Browser: Chrome Major: 65 Minor: 0


there is no my device

What I have tried:

private string getUserAgent()
       {
           if (null == HttpContext.Current || null == HttpContext.Current.Request)
               return null;


           return HttpContext.Current.Request.Headers["User-Agent"].ToString();
       }
       private string GetUserAgentInfo(string userAgentFromAPI)
       {

           userAgentFromAPI = userAgentFromAPI.Trim();
           var uaParser = Parser.GetDefault();
           ClientInfo clientInfo = uaParser.Parse(userAgentFromAPI);
           string osMajorandMinor = string.Empty;
           string browserMajorandMinor = string.Empty;
           string device = string.Empty;
           if (clientInfo.OS.Major != null) { osMajorandMinor = " Major: " + clientInfo.OS.Major + " Minor: " + clientInfo.OS.Minor; }
           if (clientInfo.UserAgent.Major != null) { browserMajorandMinor = " Major: " + clientInfo.UserAgent.Major + " Minor: " + clientInfo.UserAgent.Minor; }
           if (clientInfo.Device.Model != "" && clientInfo.Device.Brand != "") { device = " ~ Device: " + clientInfo.Device.Family; }

           StringBuilder sbUserAgent = new StringBuilder();

           sbUserAgent.Append(
               "OS: " + clientInfo.OS.Family + osMajorandMinor
               + " ~ Browser: " + clientInfo.UserAgent.Family + browserMajorandMinor
               + device);
           return sbUserAgent.ToString();
       }
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900