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

ali*_*ali 4 c# browser .net-core asp.net-core-3.0

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

我使用的代码:

var userAgent = Request.Headers["User-Agent"].ToString();
Run Code Online (Sandbox Code Playgroud)

我也尝试了这段代码,但是,它给了我错误:

UserAgent.UserAgent ua = new UserAgent.UserAgent(userAgent);
Run Code Online (Sandbox Code Playgroud)

我搜索了很多链接,但是,我没有找到我需要的内容,这是我搜索的一些链接:

  1. https://code.msdn.microsoft.com/How-to-get-OS-and-browser-c007dbf7
  2. 如何在Asp.net Core中获取用户浏览器名称( user-agent )?
  3. https://learn.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 运行应用程序的浏览器名称和版本?

Sal*_*ari 5

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

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

PM> install-package Wangkanai.Detection -pre
Run Code Online (Sandbox Code Playgroud)

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

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  
Run Code Online (Sandbox Code Playgroud)

库的安装Responsive将引入所有依赖包(这将包括Wangkanai.Detection.Device)。

PM> install-package Wangkanai.Responsive -pre
Run Code Online (Sandbox Code Playgroud)

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

install-package Wangkanai.Detection -pre 
install-package Wangkanai.Detection.Browser -pre
Run Code Online (Sandbox Code Playgroud)

然后您需要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();
}
Run Code Online (Sandbox Code Playgroud)

最后在你的 中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;
        //...
    }
} 
Run Code Online (Sandbox Code Playgroud)