Identify the UserAgent browser in the application

Source: Internet
Author: User

Asp. NET has a good mechanism to identify the user's browser: With Request.Browser can get a httpbrowsercapabilities, and then use the corresponding properties can be taken to the value of browser type, screen size, JS support and so on. For mobile devices that can be used more easily, it is easy to get the required attributes (of course, ASP.net's own browser library is still very incomplete).

But how do you identify the browser according to UserAgent in WinApp? I see through the reflector mainly in browsercapabilitiesfactory this class in the browser's capability to set, so put a part of the code inside out, found that can be used:

1string UA = "mozilla/4.0" (compatible; MSIE 7.0; Windows NT 6.0; SLCC1. NET CLR 2.0.50727;. NET CLR 

3.0.04506;. NET CLR 1.1.4322; infopath.2; MS-RTC LM 8) ";
2
3BrowserCapabilitiesFactory f = new browsercapabilitiesfactory ();
4HttpBrowserCapabilities browsercaps = new HttpBrowserCapabilities ();
5Hashtable hashtable = new Hashtable (180, stringcomparer.ordinalignorecase);
6hashtable[string. Empty] = UA;
7browsercaps.capabilities = hashtable;
8NameValueCollection headers = new NameValueCollection ();
9
10f. Configurebrowsercapabilities (headers, browsercaps);

So in the Browsercaps class to get the various properties of the browser.

Further, because ASP.net has too little browsercapabilities, if you want to add some UA to your application, especially mobile devices, you can use the. Net 2.0 supported browser add mechanism (probably Microsoft will release some of the latest mobile devices.) Browser file). The specific methods are as follows:

1, with vs Create a website, "Add asp.net folder"-"App_Browsers", download to or write their own. Put the Browser file inside.

2, Publish Web Site, to the compiled directory to find the bin directory, there is a app_browsers.dll.

3, to the C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727 directory to run:

Aspnet_regbrowsers.exe-i

So you can get an ASP in the C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\CONFIG\Browsers directory. BrowserCapsFactory.dll.

4. Refer to App_browsers.dll and ASP.BrowserCapsFactory.dll in your application and put the browsercapabilitiesfactory f = new in the top section of the code Browsercapabilitiesfactory () to be changed into:

Asp. Applicationbrowsercapabilitiesfactory f = new ASP. Applicationbrowsercapabilitiesfactory ();

You can use UA to identify the browser in your application!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.