如何获取客户端的浏览器信息?

问题描述 投票:1回答:1

如何在asp.net core 3.0.1中获取客户端的浏览器信息,我尝试使用此代码,但是,它返回了用户的完整列表浏览器,但是,我需要使用该浏览器的用户。

我使用的代码:

var userAgent = Request.Headers["User-Agent"].ToString();

我也尝试过此代码,但是,它给了我错误:

UserAgent.UserAgent ua = new UserAgent.UserAgent(userAgent);

我搜索了很多链接,但是我没有找到所需的链接,而这是我搜索过的一些链接:

  1. https://code.msdn.microsoft.com/How-to-get-OS-and-browser-c007dbf7
  2. How to get user Browser name ( user-agent ) in Asp.net Core?
  3. https://docs.microsoft.com/en-us/dotnet/api/system.web.httprequest.useragent?view=netframework-4.8
  4. https://www.c-sharpcorner.com/forums/how-to-get-current-browser-details-in-asp-net-core

有什么方法可以获取客户端从中运行应用程序的浏览器名称和版本使用Asp.Net Core 3.0.1吗?

c# browser .net-core asp.net-core-3.0
1个回答
0
投票

您可以安装Wangkanai.Detection软件包。完整的文档可以在这里找到:https://github.com/wangkanai/Detection

现在通过单个软件包完成检测库的安装参考点。

PM> install-package Wangkanai.Detection -pre

尽管如果您只需要特定的解析器,仍然可以安装单个软件包。

PM> install-package Wangkanai.Detection.Device -pre  
PM> install-package Wangkanai.Detection.Browser -pre  
PM> install-package Wangkanai.Detection.Engine -pre   //concept
PM> install-package Wangkanai.Detection.Platform -pre //concept
PM> install-package Wangkanai.Detection.Crawler -pre  

Responsive库的安装将引入所有依赖项程序包(其中将包括Wangkanai.Detection.Device

PM> install-package Wangkanai.Responsive -pre

我认为以下内容对您来说足够了:

install-package Wangkanai.Detection -pre 
install-package Wangkanai.Detection.Browser -pre

然后您需要通过在Startup.cs方法中添加检测服务来配置ConfigureServices

public void ConfigureServices(IServiceCollection services)
{
   // Add detection services container and device resolver service.
    services.AddDetection();
    services.AddDetectionCore().AddBrowser();
    // Add framework services.
    services.AddMvc();
}

最后在您的Controller中,执行以下操作:

public class HomeController : Controller
{
    private readonly IDetection _detection;

    public HomeController(IDetection detection)
    {
        _detection = detection;
    }

    public IActionResult Index()
    {
        string browser_information = _detection.Browser.Type.ToString() +
                                     _detection.Browser.Version;
        //...
    }
} 
© www.soinside.com 2019 - 2024. All rights reserved.