Directing Users to Mobile Content


One issue that comes up often is how to get users to the mobile pages once they exist. A couple of “gotchas” can complicate this apparently simple task: users having to choose between the mobile and desktop versions of content, and users being shown the wrong content for their browser type.

Theoretically, we should just be able to look at the type of browser issuing the request and redirect the user to the correct page. However, this immediately causes a problem for many users if the page they are directed to has a redirect as well. Many mobile browsers view several back-to-back redirect status codes as an error condition. Presumably they do this to avoid infinite loops, but the error also occurs in valid scenarios. If you’re using cookieless sessions, a redirect will get the session identifier in the URL. If you’re using forms authentication, the user will encounter a redirect to the login page when first accessing a protected page. If you put a couple of these scenarios together, the user will get an error message from her mobile browser when trying to access the site.

Fortunately, one approach handles this redirect problem with relatively little effort on your part: designate a mobile page as the default page. You can then use Response.Redirect to get desktop browsers to a non-mobile page. Alternatively, you can use Server.Transfer to get the appropriate page for the non-mobile device. The MobileCapabilities object has a property with the intuitive name IsMobileDevice that makes it easy to tell when a request is coming from a browser recognized as suitable for adapted content. The Page_Load method shown in the following code snippet demonstrates how a user of a non-mobile device is directed to the content designed for her particular device.

<script runat="server" language="C#">
protected void Page_Load(object o, EventArgs e) {
MobileCapabilities capabilities =
(MobileCapabilities)Request.Browser;
if(capabilities.IsMobileDevice == true) {
Server.Transfer("mobileDefault.aspx");
}
}
</script>

Using the MobileCapabilities Object

In the preceding code, notice that the HttpBrowserCapabilities object returned by the Browser property of the Request object is cast as a MobileCapabilities object. In version 1.1 of the .NET Framework, the object type associated with browser capabilities returned by the Request.Browser property is actually a MobileCapabilities object. The MobileCapabilities object inherits from the HttpBrowserCapabilities class and extends the class with support for properties that apply primarily to mobile browsers. You can see this in the browserCaps section of the machine.config file (shown in the following code), in which the result type specified is of type System.Web.Mobile.MobileCapabilities.

<browserCaps>
<result type="System.Web.Mobile.MobileCapabilities,
System.Web.Mobile, Version=1.0.5000.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a" />
</browserCaps>




Microsoft ASP. NET Coding Strategies with the Microsoft ASP. NET Team
Microsoft ASP.NET Coding Strategies with the Microsoft ASP.NET Team (Pro-Developer)
ISBN: 073561900X
EAN: 2147483647
Year: 2005
Pages: 144

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net