你将如何检测的API控制器当前的浏览器? [英] How would you detect the current browser in an Api Controller?
问题描述
我试图用MVC4在我的计划我的API控制器之一中检测当前的网络浏览器。到处看别人说用的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?
推荐答案
您可以使用的HttpBrowserCapabilities的System.Web中像这样
You can use the HttpBrowserCapabilities in System.Web like this
var userAgent = HttpContext.Current.Request.UserAgent;
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。你可以得到更多的最新定义,免费从这里 http://tempdownloads.browserscap.com/
This relies on browscap.ini in Windows/System32/inetsrv/ or Windows/SysWOW64/inetsrv for definitions. You can get more up to date definitions for free from here http://tempdownloads.browserscap.com/
本文还可以帮助 - <一个href=\"http://stephenwalther.com/archive/2010/03/05/use-asp-net-4-browser-definitions-with-asp-net-3-5\">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屋!