Extended Browser Capabilities
The MobileCapabilities class is built on the standard browser capabilities feature of ASP.NET. When a client connects to an ASP.NET mobile Web application, the ASP.NET page framework determines the requesting device from information in the request, and then attaches a MobileCapabilities object to the request. The application can access this object through the Browser property of the HttpRequest object, which is mapped to the Request property of the Page object. The application code can access individual capabilities in a MobileCapabilities object in two ways.
First, it can access any of the high-level capability properties. Each of these read-only properties returns the type-safe ** value of the corresponding capability, with an appropriate default. The following example demonstrates use of a high-level property.
[C#]
if (((MobileCapabilities)Request.Browser).ScreenCharactersWidth > 20)
{
// Coding for big screen capabilities is placed here.
}
else
{
// Coding for small screen capabilities is placed here.
}
Second, the application code can access the capabilities as a dictionary, by using the default indexer. The dictionary values are the same as those entered in the <browserCaps> section of Machine.config. The returned value is always either a string or a null reference (Nothing in Visual Basic) if the value is not set. It is the responsibility of the application to parse this string if needed. The following example demonstrates the same functionality as the previous example, using a dictionary property.
[C#]
String screenWidthText=Request.Browser["screenCharactersWidth"];
int screenWidth=(screenWidthText == null) ? 40 :
Int32.Parse(screenWidthText);
if (screenWidth > 20)
{
// This block contains code supporting a larger screen size.
}
else
{
// This block contains code supporting a smaller screen size.
}
Note MobileCapabilities properties that are derived from dictionary entries, rather than being defined in the Web.config file directly, might cause a mismatch in some devices. That is:
MobileCapabilities.ScreenCharactersWidth != MobileCapabilities["screenCharactersWidth"]