[英].NET Core 3.1 framework provided DI - how to get the instance of already registered type?
[英]How to get user device type in .net core 3.1
如果您的目标是了解用户的浏览器和有关他们设备的一些信息,那么您可以使用UAParser进行测试
它可以返回可以帮助您预测设备类型的浏览器名称和操作系统相关信息。
我尝试使用 .Net Core 6 项目进行测试并且它有效。 所以.Net Core 3.1 项目应该没有任何问题。
你可以参考下面的例子。
索引.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>
索引.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 在不同浏览器中:
此外,您可以使用不同的设备进行测试以检查结果。
参考取自此处。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.