Identify the browser corresponding to useragent in the Application

Source: Internet
Author: User

ASP. net has a good mechanism to identify users' browsers: request. browser can get an httpbrowsercapabilities, and then use the corresponding attributes to get the value, compare the browser type, screen size, JS support, and so on. For mobile devices, you can easily obtain the required attributes (of course, the browser library provided by ASP. NET is still incomplete ).

But how does one identify the browser based on useragent in winapp? I can see through reflector that the browser's capability is mainly set in the browsercapabilitiesfactory class, so part of it isCodeWe can use the following methods:

1 String UA =   " Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; Server Load balancer;. Net CLR 2.0.50727;. Net CLR 3.0.04506;. Net CLR 1.1.4322; infopath.2; MS-RTC lm 8) " ;
2
3 Browsercapabilitiesfactory F =   New Browsercapabilitiesfactory ();
4 Httpbrowsercapabilities browsercaps =   New Httpbrowsercapabilities ();
5 Hashtable =   New Hashtable ( 180 , Stringcomparer. ordinalignorecase );
6 Hashtable [ String . Empty] = UA;
7 Browsercaps. Capabilities = Hashtable;
8 Namevaluecollection Headers =   New Namevaluecollection ();
9
10 F. configurebrowsercapabilities (headers, browsercaps );

In this way, the browser attributes are obtained in the browsercaps class.

Furthermore, because ASP. NET comes with too few browsercapabilitiesProgramAdd UA identification, especially for mobile devices. You can use the browser adding mechanism supported by. NET 2.0 (Microsoft will probably release the. Browser file of some of the latest mobile devices ). The specific method is as follows:

1. Use vs to create a website, "add ASP. NET folder"-"app_browsers", and put the downloaded or self-written. Browser file into it.

2. Publish web site. Find the bin directory under the compiled directory. There is an app_browsers.dll.

3. Run the following command in the c: \ windows \ Microsoft. NET \ framework \ v2.0.50727 directory:

Aspnet_regbrowsers . Exe-I

In this way, an ASP. browsercapsfactory. dll can be obtained in the c: \ windows \ Microsoft. NET \ framework \ v2.0.50727 \ config \ browsers directory.

4. Reference app_browsers.dll and ASP. browsercapsfactory. dll in your application, and set browsercapabilitiesfactory F in the code above.= NewBrowsercapabilitiesfactory ();Changed:

ASP. applicationbrowsercapabilitiesfactory F =   New ASP. applicationbrowsercapabilitiesfactory ();

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

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.