Export (0) Print
Expand All

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 page, ASP.NET 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.

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 or an appropriate default. The following example demonstrates use of a high-level property.

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, using the default indexer. The dictionary values are the same as those in the <browserCaps> section of the Machine.config or Web.config files. The returned value is always either a string or a null reference if the value is not set. The application must parse this string if needed. The following example demonstrates the same functionality as the previous example, but uses a dictionary property.

String screenWidthText = Request.Browser["screenCharactersWidth"];
int screenWidth = 40;
if (screenWidthText != null)
    screenWidth = 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.
}
NoteNote

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.

See Also

Community Additions

ADD
Show:
© 2014 Microsoft