Mobile Redirection of ASP.NET WebForm 4.0


In most of cases , while our website domain chosen as:  http://www.mysite.com , for mobile redirection it redirected to http://www.m.mysite.com or http://mysite.com/mobile.

To remove the overhead , the http://www.mysite.com entirely will be redirected to the mobile browser view when it’s requested from mobile device browser.

  • Create an ASP.NET Web Application project in Visual Studio 2010.
  • Add an App_Browser Folder to add a directory called ‘Device’  & add ‘mobile.browser’ file from mdbf.codeplex.com.
  • Check a .class file & add the following code to it.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace MobileHandler
{
    public class MobileRedirectHandler : IHttpModule
    {
        public string RedirectToPath = "/mobile";

        public void Init(HttpApplication app)
        {
            app.BeginRequest += new EventHandler(this.OnBeginRequest);
        }
        public void Dispose() { }
        public void OnBeginRequest(object o, EventArgs e)
        {
            HttpContext context = HttpContext.Current;

            if (context.Request.Browser.IsMobileDevice &&
                context.Request.RawUrl.LastIndexOf(RedirectToPath) < 0)
                context.Response.Redirect(RedirectToPath, true);
        }
    }
} 

& make the changes in Web.Config for IIS 7.0 

<modules>
      <add name="MobileHandler" type="MobileHandler.MobileRedirectHandler, MobileHandler" preCondition="managedHandler" />
 </modules>
  • now Add New folder in project called Mobile & add Default.aspx in it.
Build & Compile the project & check the difference………………

This slideshow requires JavaScript.

Advertisements

About Anindita
Anindita Basak is working as Big Data Cloud Consultant in Microsoft. Worked in multiple MNCs as Developer & Senior Developer on Microsoft Azure, Data Platform, IoT & BI , Data Visualization, Data warehousing & ETL & of course in Hadoop platform.She played both as FTE & v- employee in Azure platform teams of Microsoft.Passionate about .NET , Java, Python & Data Science. She is also an active Big Data & Cloud Trainer & would love share her experience in IT Training Industry. She is an author, forum contributor, blogger & technical reviewer of various books on Big Data Hadoop, HDInsight, IoT & Data Science, SQL Server PDW & PowerBI.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: