Mobile Device Detection ASP.NET

In ASP.NET, you can easily detect the mobile device request using Request.Browser.IsMobileDevice property and Request.UserAgent. The following code checks the IsMobileDevice property and redirects to the mobile specific page:

      protected void Page_Load(object sender, EventArgs e)
      {
         if (Request.Browser.IsMobileDevice)
         {
             Response.Redirec("~/default_mobile.aspx");          
         }
      }
In MVC3 exposes an IsMobileDevice flag in the Request.Browser object. So in your razor code, you can query this variable and render accordingly. For example,

      @if (Request.Browser.IsMobileDevice) {
            HTML here for mobile device
         } else {
               HTML for desktop device
         }

Comments

Popular posts from this blog

C# Crop white space from around the image

Could not load file or assembly 'Microsoft.ReportViewer.Common, Version=xx.0.0.0, Culture=neutral, PublicKeyToken='xxx' or one of its dependencies.

The specified version string contains wildcards, which are not compatible with determinism.