Redirecting to an ASP.NET Mobile Web Page

If you create an application that has one user interface optimized for a desktop browser and a second user interface optimized for mobile applications, you need a mechanism to redirect mobile device customers to the mobile Web pages.

Because some devices do not support cookies, consider whether you want to rely on cookies for your application. For more information, see Controlling Session State in ASP.NET Mobile Web Pages.

If your application does not use cookies, you can use the following code in a Microsoft ASP.NET site to redirect to an ASP.NET mobile Web application.

<script runat="server" language="c#">
    public void Page_Load(Object sender, EventArgs e) 
    {
        if (Request.Browser["IsMobileDevice"] == "true" ) 
        {
            Response.Redirect("MobileDefault.aspx");
        }
        else 
        {
            Response.Redirect("DesktopDefault.aspx");
        }
    }
</script>

Because some devices do not accept relative URLs, you must also set the useFullyQualifiedRedirectUrl attribute of the <httpRuntime> element to true in the Web.config file. This sends a fully qualified URL to the client with the session ID appended to the end of the URL. Specifying a relative URL and then converting that URL to a fully qualified URL is necessary to preserve session state. The following example shows the configuration setting.

<configuration>
  <system.web>
    <httpRuntime useFullyQualifiedRedirectUrl = "true" />
  </system.web>
</configuration>

See Also

Reference

httpRuntime Element (ASP.NET Settings Schema)

Concepts

User Controls

Controlling Session State in ASP.NET Mobile Web Pages

Other Resources

Application Developer's Guide

Developing ASP.NET Mobile Web Pages