您将如何检测 Api 控制器中的当前浏览器? [英] How would you detect the current browser in an Api Controller?
问题描述
我正在尝试使用 MVC4 在我的程序中的一个 Api 控制器中检测当前的 Web 浏览器.在我看到的任何地方,人们都说要使用 Request.Browser
,但是我无法让它发挥作用.有什么建议或者我忽略了什么吗?
I'm trying to detect the current web browser within one of my Api Controllers in my program using MVC4. Everywhere I look people say to use Request.Browser
, however I can't get that to work. Any suggestions or is there something I'm overlooking?
推荐答案
你可以像这样使用 System.Web 中的 HttpBrowserCapabilities
You can use the HttpBrowserCapabilities in System.Web like this
var userAgent = HttpContext.Current.Request.UserAgent;
var userBrowser = new HttpBrowserCapabilities { Capabilities = new Hashtable { { string.Empty, userAgent } } };
var factory = new BrowserCapabilitiesFactory();
factory.ConfigureBrowserCapabilities(new NameValueCollection(), userBrowser);
//Set User browser Properties
BrowserBrand = userBrowser.Browser;
BrowserVersion = userBrowser.Version;
这依赖于 Windows/System32/inetsrv/或 Windows/SysWOW64/inetsrv 中的 browscap.ini 进行定义.
This relies on browscap.ini in Windows/System32/inetsrv/ or Windows/SysWOW64/inetsrv for definitions.
这篇文章也可能有帮助 - http://stephenwalther.com/archive/2010/03/05/use-asp-net-4-browser-definitions-with-asp-net-3-5
This article may also help - http://stephenwalther.com/archive/2010/03/05/use-asp-net-4-browser-definitions-with-asp-net-3-5
这篇关于您将如何检测 Api 控制器中的当前浏览器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!