// 注册服务 using Microsoft.AspNetCore.Http; services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
// 实现层 // 注入引用 private readonly IHttpContextAccessor _httpContextAccessor; _httpContextAccessor = serviceProvider.GetService<IHttpContextAccessor>(); // 方法内 var headers = _httpContextAccessor.HttpContext.Request.Headers; string str_u = headers["User-Agent"].FirstOrDefault();
// 简易判断
string[] keywords = { "Android", "iPhone", "iPod", "iPad", "Windows Phone", "MQQBrowser" }; var clientType = keywords.Where(x => str_u.IndexOf(x) > -1).Any() ? "Mobile" : "PC";
标签:core,httpContextAccessor,string,接口,var,keywords,net From: https://www.cnblogs.com/benneryang/p/16617178.html