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!