How would you detect the current browser in an Api Controller?

后端 未结 2 394
-上瘾入骨i
-上瘾入骨i 2020-12-17 10:06

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, h

相关标签:
2条回答
  • 2020-12-17 10:56

    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;
    

    This relies on browscap.ini in Windows/System32/inetsrv/ or Windows/SysWOW64/inetsrv for definitions.

    This article may also help - http://stephenwalther.com/archive/2010/03/05/use-asp-net-4-browser-definitions-with-asp-net-3-5

    0 讨论(0)
  • 2020-12-17 10:59

    You could do something like following too from within the Web API's action:

    System.Net.Http.HttpRequestMessage currentRequest = this.Request;
    System.Net.Http.Headers.HttpHeaderValueCollection<System.Net.Http.Headers.ProductInfoHeaderValue> userAgentHeader = currentRequest.Headers.UserAgent;
    
    0 讨论(0)
提交回复
热议问题