我如何在asp.net core 3.0.1中获取客户端的浏览器信息,我尝试使用此代码,但是,它返回了用户浏览器的完整列表,但是,我需要该用户使用的浏览器。
我使用的代码:
var userAgent = Request.Headers["User-Agent"].ToString();
我也尝试了这段代码,但是,它给了我错误:
UserAgent.UserAgent ua = new UserAgent.UserAgent(userAgent);
我搜索了许多链接,但没有找到所需的链接,而这是我搜索过的一些链接:
是否有任何方法可以获取客户端 使用Asp.Net Core 3.0.1从中运行应用程序的浏览器名称和版本?
您可以安装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;
//...
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句