Redirecting to a Mobile Web Application

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 site.

Because some devices do not support cookies, consider whether you want to rely on cookies for your application. For more information about using cookies, see Controlling Session State.

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 useFullyQualifiedRedirectUrl attribute 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

User Controls | <httpRuntime> Section | Controlling Session State | Application Developer's Guide | Developing ASP.NET Mobile Web Applications