This is really odd, I'm using the HttpContext.Request.Browser.Browser
property to check from which browser the request came for.
When using chrome, the value is Chrome
When using firefox, the value is Firefox
When using Edge, the value is Chrome
Is it a known bug in HttpContext
?
What is the most accurate way to detect IE\Edge users? I've seen many JS codes which checks the user_agent
value, but it keeps changing with every IE version so it is really hard to know which code is updated, and which one isn't.
Maybe there's some good JS library for that purpose that someone can please recommend?
答案 0 :(得分:3)
这种方法适合我
if (Regex.IsMatch(HttpContext.Request.UserAgent, @"Edge\/\d+"))
{
wrkBrowser = "Edge"
}
如果您要检查多个浏览器,请注意您检查的顺序,因为许多浏览器都喜欢在其UserAgent字符串中提及其他浏览器。 那时检查以下条件
Request.Browser.Browser == "Chrome"
答案 1 :(得分:1)
我遇到了同样的问题,并且找出了根本原因并能够解决。
解决方案是增加Web.config中的userAgentCacheKeyLength
,如下所示:
<system.web>
<browserCaps userAgentCacheKeyLength="256" />
</system.web>
默认情况下,userAgentCacheKeyLength
的值为64。
发生的事情是IIS将用户代理值缓存到userAgentCacheKeyLength
中定义的长度(默认为64)。因此,如果您使用Chrome浏览器发出第一个请求,则IIS会在缓存中存储Chrome用户代理值的前64个字符。现在,下次在您遇到Edge发出的请求时,IIS会匹配前64个字符,并且与Chrome相似,因此IIS将浏览器的值返回为Chrome。
如果将该值增加到256,则IIS将能够存储用户代理的全部值,因此它将始终准确地区分Chrome和Edge浏览器。
答案 2 :(得分:0)
如果您不介意使用JS,可以执行以下操作:
function get_browser() {
var ua = navigator.userAgent, tem, M = ua.match(/(opera|chrome|safari|firefox|msie|trident|edge(?=\/))\/?\s*(\d+)/i) || [];
if (/trident/i.test(M[1])) {
tem = /\brv[ :]+(\d+)/g.exec(ua) || [];
return { name: 'IE', version: (tem[1] || '') };
}
if (M[1] === 'Chrome') {
tem = ua.match(/\bOPR\/(\d+)/);
if (tem != null) { return { name: 'Opera', version: tem[1] }; }
tem = ua.match(/\bEdge\/(\d+)/);
if (tem != null) { return { name: 'Edge', version: tem[1] }; }
}
M = M[2] ? [M[1], M[2]] : [navigator.appName, navigator.appVersion, '-?'];
if ((tem = ua.match(/version\/(\d+)/i)) != null) { M.splice(1, 1, tem[1]); }
return {
name: M[0],
version: M[1]
};
}
然后你可以简单地调用它并将结果发送到你的后端。它对我来说就像一个魅力。