Wednesday, May 6, 2015

Detect browser language and other information in ASP.NET

If you just want to detect browser language you can use this

  CultureInfo ci;  
       if (userLanguages.Count() > 0)  
       {  
         try  
         {  
           ci = new CultureInfo(userLanguages[0]);  
         }  
         catch (CultureNotFoundException)  
         {  
           ci = CultureInfo.InvariantCulture;  
         }  
       }  
       else  
       {  
         ci = CultureInfo.InvariantCulture;  
       }  

if you want to get more details then use this


   string browser = HttpContext.Request.Browser.Browser;  
       string version = HttpContext.Request.Browser.Version;  
       string type = HttpContext.Request.Browser.Type;  
       string platform = HttpContext.Request.Browser.Platform;  
       string userAgent = HttpContext.Request.UserAgent;  
       string[] userLang = HttpContext.Request.UserLanguages;  
       var userLanguages = Request.UserLanguages;  


For MVC use this in your Home controller action.



Ensure that this project has Microsoft.Bcl.Build installed and packages.config is located next to the project file

In my Xamarin form application their was dependency Restclient on Microsoft.Bcl.Build Microsoft.Bcl. i just remove all using nuget and ...