简体   繁体   English

如何在.net core 3.1中获取用户设备类型

[英]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?你能告诉我如何在 .net 核心 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如果您的目标是了解用户的浏览器和有关他们设备的一些信息,那么您可以使用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.我尝试使用 .Net Core 6 项目进行测试并且它有效。 So it should not have any issue with the.Net Core 3.1 project.所以.Net Core 3.1 项目应该没有任何问题。

You could refer to the example below.你可以参考下面的例子。

Index.cshtml索引.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索引.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: Output 在不同浏览器中:

在此处输入图像描述

Further, you could make a tests with different devices to check for the results.此外,您可以使用不同的设备进行测试以检查结果。

Reference is taken from here .参考取自此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 .NET Core 3.1 框架提供 DI - 如何获取已注册类型的实例? - .NET Core 3.1 framework provided DI - how to get the instance of already registered type? ASP.NET Core3.1登录后如何获取用户信息 - How to get User Information after login in ASP.NET Core3.1 如何检查用户是否处于单一角色? 在 .NET Core 3.1 中使用身份 - How to check if a User is in a single role? Using Identity in .NET Core 3.1 如何从 Azure AD B2C 用户商店获取 ASP.NET Core 3.1 中的用户信息? - How to get user information in ASP.NET Core 3.1 from Azure AD B2C user store? 如何从 .NET Core 3.1 中的 DI 获取 SignalR IHubContext? - How to get SignalR IHubContext from DI in .NET Core 3.1? 如何使用 Serilog 获取 Json 中的日志级别 - .NET Core 3.1 - How To Get Log Level in Json with Serilog - .NET Core 3.1 如何在 ASP.NET Core 3.1 中获取当前的 JsonSerializerOptions? - How to get current JsonSerializerOptions in ASP.NET Core 3.1? 如何让 .Net Core 3.1 查看我的 appsettings 文件 - How to get .Net Core 3.1 to see my appsettings files 如何在 .Net Core 3.1 Web API 控制器中获取声明? - How to get claims in .Net Core 3.1 Web API Controller? 如何使用 ASP.Net Core Identity 3.1 从 controller 中的登录用户获取 Google 和 Facebook 个人资料图片? - How can I get Google and Facebook profile picture from logged in user in a controller with ASP.Net Core Identity 3.1?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM