ead*_*dam 73 asp.net-core-mvc asp.net-core
能告诉我如何获取客户端在MVC 6,asp.net 5中使用的浏览器的名称吗?
ead*_*dam 116
我觉得这很简单.得到了答案Request.Headers["User-Agent"].ToString()
.
Uza*_*zay 11
userAgent = Request.Headers["User-Agent"];
Run Code Online (Sandbox Code Playgroud)
https://code.msdn.microsoft.com/How-to-get-OS-and-browser-c007dbf7
Ane*_*han 11
对我来说,没有Request.Headers["User-Agent"].ToString()
帮助返回所有浏览器的名称,因此在解决方案后找到了它。
安装了ua-parse。在控制器中using UAParser;
var userAgent = httpContext.Request.Headers["User-Agent"];
string uaString = Convert.ToString(userAgent[0]);
var uaParser = Parser.GetDefault();
ClientInfo c = uaParser.Parse(uaString);
Run Code Online (Sandbox Code Playgroud)
使用上面的代码后,可以通过使用userAgent获取浏览器详细信息,c.UserAgent.Family
还可以像c.OS.Family;
我已经开发了一个库来扩展ASP.NET Core,以支持Wangkanai.Detection中的 Web客户端浏览器信息检测。这应该可以让您标识浏览器名称。
namespace Wangkanai.Detection
{
/// <summary>
/// Provides the APIs for query client access device.
/// </summary>
public class DetectionService : IDetectionService
{
public HttpContext Context { get; }
public IUserAgent UserAgent { get; }
public DetectionService(IServiceProvider services)
{
if (services == null) throw new ArgumentNullException(nameof(services));
this.Context = services.GetRequiredService<IHttpContextAccessor>().HttpContext;
this.UserAgent = CreateUserAgent(this.Context);
}
private IUserAgent CreateUserAgent(HttpContext context)
{
if (context == null) throw new ArgumentNullException(nameof(Context));
return new UserAgent(Context.Request.Headers["User-Agent"].FirstOrDefault());
}
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
41650 次 |
最近记录: |