简体   繁体   中英

How to get user device type in .net core 3.1

Can you please let me know how to get the device type in .net core 3.1? I have tried the below sample but it gives me all available options.

this.Request.HttpContext.GetServerVariable("HTTP_USER_AGENT");

在此处输入图像描述

If your goal is to know the user's browser and some information about their device then you could make a test with the UAParser

It could return browser names and OS-related information that could help you predict the device type.

I tried to make a test using.Net Core 6 project and it works. So it should not have any issue with the.Net Core 3.1 project.

You could refer to the example below.

Index.cshtml

@page
@model IndexModel
@{
    ViewData["Title"] = "Home page";
}

<div class="text-center">
    <h1 class="display-4">Welcome</h1>
    <p>Learn about <a href="https://learn.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
     <p>You are using @Model.Message</p>
</div>

Index.cshtml.cs

   public string Message { get; set; }
    public void OnGet()
    {
        var userAgent = HttpContext.Request.Headers["User-Agent"];
        var uaParser = Parser.GetDefault();
        ClientInfo c = uaParser.Parse(userAgent);
        this.Message = c.ToString();
    }

Output in different browsers:

在此处输入图像描述

Further, you could make a tests with different devices to check for the results.

Reference is taken from here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM