Determine the function of the browser in ASP.NET

xiaoxiao2021-03-06  39

This page

HTTP header and user agent Determine browser features to map headers to browser feature superior browser and subsequent browser define new browser tests to create their own target HTMLTextWriter and HTMLTextWriter32 mobile browser features Summary

The web application is different from other applications running in homologous environments, as the former will send the output to a wide variety of platforms and web browsers. Some browsers support the client script, some support XHTML, and some will limit the screen area. So how do your web application pass the content to a restriction function or a special requirements?

In order to answer this question, I will study the support of ASP.NET 1.1 to determine the functionality of the browser. To see the upcoming features in ASP.NET 2.0, see the Browser Bar "ASP.NET 2.0 Browser". [Edit Update - December 14, 2004: The bar bar has been updated. ]

HTTP header and user agent

Determine the first step in the browser feature that will be sent to your web site is to identify which type of browser belongs to. The payload sent in the HTTP header contains a user agent string that describes a request for a browser. Although, you can usually see the header sent by the client browser to the remote web site, but you can use the TCP trace application to view. I use TCPTrace (an application written by Simon Fell and Matt Humphrey) to verify the header. Figure 1 shows a header dump of a web request.

Note the user agent string in Figure 2. In your ASP.NET application, you can easily extract strings from the header collection by programming. You can also view the user agent string as shown in Figure 3 when tracing is enabled in your application.

Figure 3 shows the http_user_agent string

This is smooth, but what can you use this information? They will only be useful when you know what is added to each browser version. In traditional ASP and ASP.NET, various browser functions are recorded in the auxiliary file.

Back to top

Determine the browser function

In traditional ASP, DLL% windir% / system32 / inetsrv / browscap.dll contains a class that determines the functionality of the browser based on incoming user agent strings. The browser function object checks% windir% / system32 / inetsrv / browscap.ini to match the user agent string with the .ini file, which describes some of the well-known functions of this browser. (The ATL server is a C template library for creating an ISAPI DLL. It also uses browscap.ini to determine the browser feature. For more information on the ATL server, check the ASP column in the November 2003 issue of MSDN® Magazine.)

Figure 4 shows a part of the browscap.ini file that illustrates your ability to identify your browser for Microsoft® Internet Explorer 2.0. This part. INI file indicates that Internet Explorer does not support client scripts and does not support Java applets.

To use information in the traditional ASP application, you must create a browsertype object in the script (it is a COM object with "mswc.browsertype") and queries its properties to get the name, version, and Whether to support cookie and other information. You can accurately transfer your content through this information.

The ASP.NET contains the HTTPBrowserCapabilities class for querying browser features. The HTTPRequest class contains references to instances of the HTTPBrowserCapAbilties class, so it is usually available throughout the application. You can extract it from the current instance of the current Page instance or the HTTPContext class. HttpBrowserCapabilities contains many properties and its meaning, as you can see in Figure 5. Most browser features are obvious, you can use the information provided to adjust your output. For example, if you have a script, you only send it to the client when the other end browser understands it, and I use the code in Figure 6.

Back to top

Map the header to the browser function

In the September issue of this column, I saw ASP.NET configuration and how ASP.NET uses a specific handler to interpret individual parts in the configuration file. Now let's take a look at the various parts of the Machine.config file, the machine.config file is the starting point configured in the ASP.NET. At the location close to the top of the file, you will see a node that provides a component name that explains the user agent string to determine the functionality of the browser:

TYPE = "System.Web.configuration.httpcapabilitiessectionHandler,

System.Web, Version = 1.0.5000.0, Culture = neutral,

PublicKeyToken = B03F5F7F11D50A3A "/>

...

As you can see, the type named "httpcapabilitiessectionhandler" is used to parse the section of the configuration file. After parsing the browser configuration settings, for each request, it views the HTTP header and uses them to determine the client browser. elements in Machine.config include mapping between HTTP headers and specific browser functions. Each request uses this information to populate the httpbrowsercapability class. Note that the browser feature does not match only the user agent string. Any inbound header can match and serve as part of the value set for the HTTPBrowSerCapabilities object. Figure 7 shows some of the code in the section of the Machine.config file.

The section defines a set of key values ​​that represent features. After you go deep into the Machine.config file, you will see mappings between these variables and a particular regular expression in a variety of user proxy strings. For example, you will see a regular expression that represents a client version between the tag. If httpbrowsercapabilities matches the platform string in the user agent string, the HTTPBROWSERCAPABILISIESHANDLER sets the platform properties correctly:

Platform = WinXP

Platform = Win2000

Back to top

The superior browser is as defined in Machine.config, as defined in Machine.config, is divided into two main types: superiors and subordinates. The upper browser supports HTML 4.0, a stacked style sheet, ECMAScript 1.2 (JScript® and JavaScript), and a Document Object Model with W3C compatible document object. The lower browser and client devices support only HTML 3.2. Using the difference between the browser, how the Web site will change when accessing the Web site in the most common use case.

Most modern browsers are mentioned in Machine.Config. If a browser is not defined there, you can easily add the definition to the web.config for your application.

Back to top

Define new browsers

As almost all other configuration settings mentioned in Machine.Config are extension, the browser's function settings can also be extended. Suppose there is an unknown browser that you need to tailor your site. In order to support the functionality of the new browser, create a separate section in your web.config file, as shown in Figure 8.

This is used to tell ASP.NET has a new browser called "LameBrowser". A definition is given in the section, which is a sub-browser that supports background music. If the user agent contains a string "LameBrowser", ASP.NET will fill the current HttpBrowserCapabilities object with the values ​​mentioned in Web.config. Note that although the matching regular expression is additional, the subsequent further matches in the application hierarchy will override these settings.

Back to top

test

If you have a replica of the target browser in your test environment, you can easily understand how your Web site responds. If not, ASP.NET supports an aliasing technology that enables you to force an application to think that a browser has issued a request.

You will find the section in Machine.Config, which defines the alias of Internet Explorer 4.0 and 5.0 and superiors and sub-browsers:

"Mozilla / 4.0 (Compatible; Msie 5.5; Windows NT 4.0)" />

"Mozilla / 4.0 (Compatible; Msie 4.0; Windows NT 4.0)" />

"Mozilla / 4.0 (Compatible; Msie 4.0; Windows NT 4.0)" />

As you can see, the section will abbreviate mapping to a specific user agent string. If you set the page's ClientTarget property to any alias listed, the ClientTargetSectionHandler class will intercept the alias and use a specific user agent string to determine the functionality of the browser. Below is an example of setting a clientTarget in the page instruction (you can also set this property in the Visual Studio designer): <% @ page language = "c #" codeBehind = "Webform1.aspx.cs"

Inherits = "browsercapabilitysite.webform1" ClientTarget = "UPLEVEL"%>

This allows you to display the page presenting in different browsers.

Back to top

Create your own goal

Although ASP.NET provides you with four built-in target alias, you may benefit from more aliases. For example, suppose you want to test your site via the Firefox browser or the LameBrowser mentioned earlier. The XML bits in your web.config file will complete this task below (assuming that you have set each ):

Back to top

HtmlTextWriter and HtmlTextWriter32

In addition to the properties shown in Figure 5, HttpBrowSerCapabilities also discloses another read-only attribute called TagWrite. TagWriter property Open class, used to present tags in the control. If you have written custom server-side controls, you have no doubt that HTMLTextWriter classes that are passed to the Render method.

You may think that from the server-side control call Output.write is the same as that call response.write, but in fact, the former is more flexible than the latter. HTMLTextWriter contains methods for presenting a particular HTML element.

Although the other ASP.NET server-side controls are usually very good when writing custom controls, but sometimes you need to manually process tags. Some methods including the HTMLTextWriter class can support stack rendering tags and properties.

In addition to saving you to save start and end tags, HTMLTextWriter can help you publish the correct version of HTML. When ASP.NET gets a request, it checks the TagWriter property of the HTTPBrowserCapabilities class and uses HTMLTextWriter or HTML32TextWriter (HTML32TextWriter to HTMLTextWriter). For example, in HTML 3.2 and HTML 4.0, the presentation will be slightly different. The code segment in Figure 9 (the render method overwritten in the server-side control) presents a simple table with only one line and a column. HTMLTextWriter includes a method of rendering attributes and tags based on the HTML version of the browser. Below is the way HTML32TextWriter rendered table

Hi there ...

The HTMLTextWriter class presents a table as follows:

>

Hi there ...

Keep in mind that Page is derived in the Control class and you can rewrite the render method for HTMLTextWriter from Page. Build an ASP.NET standard server-side control to render according to the function of issuing a request.

Back to top

Mobile browser function

So far, we only consider basic browser features. Starting from ASP.NET 1.1, support a new new mobile browser. Traversing the entire Machine.config file installed on ASP.NET 1.1, reveals settings such as Windows® CE, NOKIA, Ericsson and Avantgo and other devices and systems. You will find that they are built in a browser in modern mobile phones and PDAs.

ASP.NET determines these functions with identical methods as the general browser. However, when you review a variety of different devices, you will see that the browser features of these devices will be more difficult because more features need to be clarified.

Determining the mobile function of the device is just a type conversion from the normal device function. That is, you only need to convert the Request.Browser field of the MobileCapabilities class, you can check the device function (MobileCapabilities is derived from httpbrowsercapability). The code in Figure 10 determines the nature of the browser / device (WML or HTML) tag language, the size of the user screen (in character), and whether the device can process emails.

When you see Machine.config, you will see more than 75 mobile device features (except for standard browser features). The mobile function of the device is configured by the MobileDeviceCapabilitiessectionHandler configuration handler.

Back to top Summary

ASP.NET provides a number of features that can be used to determine the functionality of the client that issues a given request. This provides you with the means of controlling site output, and the method is to specify the alias of the client target as a specific user agent string in the application.

Please send your questions and comments to asp-net@microsoft.com.

George Shepherd is specially engaged in the development of .NET Framework software. He is the author of the book of Programming With Microsoft Visual C . Net (Microsoft Press, 2002), and with others, a book with Applied .NET (AdDison-Wesley, 2001). He is a architect contributing to the SyncFusion tool in Windows Forms and ASP.NET tools.

转载请注明原文地址:https://www.9cbs.com/read-60095.html

New Post(0)
CopyRight © 2020 All Rights Reserved
Processed: 0.034, SQL: 9