2

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.

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Request.Browser is not available in ASP.NET Web API application. I think you are talking about a ASP.NET Web Application. I want to detect consumer's device information in my ASP.NET Web API application.
1
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

Comments

0

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

2 Comments

I need to detect device information (Consumer/Requestor of my Web API) in a ASP.NET Web API not in a Console Application.
@user1780538 although this looks like a console app, it is in fact how to access the variables in a .Net Core Web API project.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.