Online Book Reader

Home Category

Programming Microsoft ASP.NET 4 - Dino Esposito [182]

By Root 5328 0
surely provide valuable content and services, but that might not be good enough if that content and those great services are hard to find, understand, and consume.

There are three fundamental items that any developers of any Web sites must tick off their to-do list as soon and as as possible: consistent cross-browser display, Search Engine Optimization (SEO), and site navigation. Cross-browser display ensures that your pages will look and work the same regardless of the device being used to reach it. SEO best practices ensure that your site is ranked high by search engines and possibly appear as one of the first links when a user searches for a related keyword. Finally, once users arrive, they must be able to work with the site seamlessly and have an enjoyable experience. Site navigation facilities are fundamental.

In the rest of this chapter, I’ll address some of the best practices and ASP.NET techniques and technologies to provide users with a consistent and comfortable experience while interacting with the site.

Cross-Browser Rendering


Although all client browsers these days share a common set of capabilities large enough to implement nice Web features, the old motto of “Write once, browse everywhere” is a fairy tale. That a page works the same across different browsers is not a foregone conclusion; rather, it’s something you have to test carefully and that might require a bit of extra work to achieve. Especially with extremely dynamic pages full of script and HTML manipulation code, the risk of having some markup misinterpreted is real.

Cross-browser rendering refers to the set of techniques and technologies you can use to ensure that your pages work and look the same regardless of the browser in use. The key idea behind cross-browser rendering is that the code within the page is able to detect the browser ID and its known set of capabilities. Based on that, the code within the page will then work out a solution to get the best possible markup for the device.

ASP.NET provides a specific API to detect browser capabilities programmatically and also to keep the set of capabilities updated over time.

Detecting Browser Capabilities


In ASP.NET, the central repository for browser information is the Browser property on the HttpRequest object. Here’s how it is defined:

public HttpBrowserCapabilities Browser

{

get { ... }

set { ... }

}

When the getter method is invoked for the first time, the HttpRequest object gets and caches any available browser information. The user agent information carried by the request is used to identify the requesting browser. Any gathered browser information is published through an instance of the HttpBrowserCapabilities class. The HttpBrowserCapabilities class groups, in a single place, values that identify a fair number of browser capabilities, including support for ActiveX controls, scripting languages, frames, cookies, and much more. Note that no information is in any way dynamically set by the browser; instead, it is retrieved from an offline server-side repository.

As mentioned, ASP.NET identifies the connected browser by reading the user-agent information that is passed during a request. ASP.NET compares the user-agent string that is received from the browser to user-agent strings that are stored in server-side browser definition files. These files contain information about the known capabilities of various user agents. When ASP.NET finds a match between the current user-agent string and a user-agent string in a browser definition file, it loads the corresponding browser capabilities into the HttpBrowserCapabilities object. The following code shows how to identify and output the name of the calling browser:

var browserCaps = Request.Browser;

Label1.Text = browserCaps.Browser;

The properties of the HttpBrowserCapabilities object can then be used to determine whether the browser type that is represented by the user agent supports scripting, styles, frames, and so on. Based on these capabilities, the controls on the page render Web controls using appropriate markup.

Return Main Page Previous Page Next Page

®Online Book Reader