在普通的asp.net中,以下工作:
private float getInternetExplorerVersion()
{
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
float rv = -1;
System.Web.HttpBrowserCapabilities browser = Request.Browser;
if (browser.Browser == "IE")
rv = (float)(browser.MajorVersion + browser.MinorVersion);
return rv;
}
但是在asp.net中,mvc2在“System.Web.HttpBrowserCapabilities browser = Request.Browser;”中出错:
无法将类型'System.Web.HttpBrowserCapabilitiesBase'隐式转换为'System.Web.HttpBrowserCapabilities'
由于 ARNAB
答案 0 :(得分:6)
这是因为Browser属性的类型为HttpBrowserCapabilitiesBase
,而不是HttpBrowserCapabilities
。因此要么使用隐式var
,要么指定正确的类型:
float rv = -1;
var browser = Request.Browser;
if (browser.Browser == "IE")
rv = (float)(browser.MajorVersion + browser.MinorVersion);
或者如果您更喜欢输入大量代码:
float rv = -1;
System.Web.HttpBrowserCapabilitiesBase browser = Request.Browser;
if (browser.Browser == "IE")
rv = (float)(browser.MajorVersion + browser.MinorVersion);