为了保持与现有代码的兼容性,我创建了一个自定义提供程序,以便Request.Browser
按预期返回信息。例如,Browser.Browser
将是“IE”而不是“InternetExplorer”,这是安装修补程序后的新值。
此外,此方法在兼容性视图中返回 IE 的实际版本,而不是版本 7。请注意,Browser.Type
在兼容性视图中将返回“IE7”以防您需要检测它,或者您可以轻松修改自定义提供程序以更改 .Type 。
方法很简单。从您的类的实例派生一个类HttpCapabilitiesDefaultProvider
并将其设置BrowserCapabilitiesProvider
为一个实例。
在 Global.asax.cs 中:
protected void Application_Start(Object sender, EventArgs e)
{
...
HttpCapabilitiesBase.BrowserCapabilitiesProvider = new CustomerHttpCapabilitiesProvider();
...
}
定义你的班级:更新包括微软边缘浏览器
public class CustomerHttpCapabilitiesProvider : HttpCapabilitiesDefaultProvider
{
public override HttpBrowserCapabilities GetBrowserCapabilities(HttpRequest request)
{
HttpBrowserCapabilities browser = base.GetBrowserCapabilities(request);
// Correct for IE 11, which presents itself as Mozilla version 0.0
string ua = request.UserAgent;
// Ensure IE by checking for Trident
// Reports the real IE version, not the compatibility view version.
if (!string.IsNullOrEmpty(ua))
{
if (ua.Contains(@"Trident"))
{
if (!browser.IsBrowser(@"IE"))
{
browser.AddBrowser(@"ie");
browser.AddBrowser(@"ie6plus");
browser.AddBrowser(@"ie10plus");
}
IDictionary caps = browser.Capabilities;
caps[@"Browser"] = @"IE";
// Determine browser version
bool ok = false;
string majorVersion = null; // convertable to int
string minorVersion = null; // convertable to double
Match m = Regex.Match(ua, @"rv:(\d+)\.(\d+)");
if (m.Success)
{
ok = true;
majorVersion = m.Groups[1].Value;
minorVersion = m.Groups[2].Value; // typically 0
}
else
{
m = Regex.Match(ua, @"Trident/(\d+)\.(\d+)");
if (m.Success)
{
int v;
ok = int.TryParse(m.Groups[1].Value, out v);
if (ok)
{
v += 4; // Trident/7 = IE 11, Trident/6 = IE 10, Trident/5 = IE 9, and Trident/4 = IE 8
majorVersion = v.ToString(@"d");
minorVersion = m.Groups[2].Value; // typically 0
}
}
}
if (ok)
{
caps[@"MajorVersion"] = majorVersion;
caps[@"MinorVersion"] = minorVersion;
caps[@"Version"] = String.Format(@"{0}.{1}", majorVersion, minorVersion);
}
}
else if (ua.Contains(@"Edge"))
{
if (!browser.IsBrowser(@"Edge"))
{
browser.AddBrowser(@"edge");
}
IDictionary caps = browser.Capabilities;
caps[@"Browser"] = @"Edge";
// Determine browser version
Match m = Regex.Match(ua, @"Edge/(\d+)\.(\d+)");
if (m.Success)
{
string majorVersion = m.Groups[1].Value;
string minorVersion = m.Groups[2].Value;
caps[@"MajorVersion"] = majorVersion;
caps[@"MinorVersion"] = minorVersion;
caps[@"Version"] = String.Format(@"{0}.{1}", majorVersion, minorVersion);
}
}
}
return browser;
}
}