user1780538
user1780538

Reputation: 970

How to detect device information like OS, browser, device name etc. in asp.net web api?

I am creating a ASP.NET Web API. I want to detect device information of consumer of my web service. Currently, I am trying to use Request.Headers.UserAgent to get device related information.

public void XYZ(int a, int b)
{            
        var x = Request.Headers.UserAgent;
}

But unable to get the proper information.

Upvotes: 2

Views: 14140

Answers (3)

Anup Sharma
Anup Sharma

Reputation: 2083

Request.Headers["User-Agent"]

Provides you the user agent information. Typically the value looks like:

Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36

Upvotes: 1

Vishnu Prasad V
Vishnu Prasad V

Reputation: 1248

You can use HttpBrowserCapabilities class for this in ASP.NET. Just get the Browser property of the Request you receive.

HttpBrowserCapabilities capability= Request.Browser;
var BrowserName = capability.Browser;
var version = capability.Version;
var platform = capability.Platform;

HttpBrowserCapabilities belongs to System.Web namespace.

Hope this helps.

Upvotes: 3

themattkellyshow
themattkellyshow

Reputation: 161

Here you are sir.

public static void Main(string[] args)
{
    IRuntimeEnvironment runtime = PlatformServices.Default.Runtime;
    IApplicationEnvironment env = PlatformServices.Default.Application;
    Console.WriteLine($@"
IApplicationEnvironment:
        Base Path:      {env.ApplicationBasePath}
        App Name:       {env.ApplicationName}
        App Version:    {env.ApplicationVersion}
        Runtime:        {env.RuntimeFramework}
IRuntimeEnvironment:
        OS:             {runtime.OperatingSystem}
        OS Version:     {runtime.OperatingSystemVersion}
        Architecture:   {runtime.RuntimeArchitecture}
        Path:           {runtime.RuntimePath}
        Type:           {runtime.RuntimeType}
        Version:        {runtime.RuntimeVersion}");
}

Upvotes: 0

Related Questions