Detect mobile browser or web browser and mobile device using C# and VB.Net

DewaCTAgro
 
on Oct 24, 2016 05:42 AM
5953 Views

Why Mobile device like Asus Zenfone 5 not ditected by regex ?

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Oct 24, 2016 05:51 AM

Hi Dewa_CTAgro,

Here the below code detect user was browsing from a mobile device or a normal web browser and redirect to the appropriate version.

C#

public static bool isMobileBrowser()
{
    //GETS THE CURRENT USER CONTEXT
    HttpContext context = HttpContext.Current;

    //FIRST TRY BUILT IN ASP.NT CHECK
    if (context.Request.Browser.IsMobileDevice)
    {
        return true;
    }
    //THEN TRY CHECKING FOR THE HTTP_X_WAP_PROFILE HEADER
    if (context.Request.ServerVariables["HTTP_X_WAP_PROFILE"] != null)
    {
        return true;
    }
    //THEN TRY CHECKING THAT HTTP_ACCEPT EXISTS AND CONTAINS WAP
    if (context.Request.ServerVariables["HTTP_ACCEPT"] != null &&
        context.Request.ServerVariables["HTTP_ACCEPT"].ToLower().Contains("wap"))
    {
        return true;
    }
    //AND FINALLY CHECK THE HTTP_USER_AGENT HEADER VARIABLE FOR ANY ONE OF THE FOLLOWING
    if (context.Request.ServerVariables["HTTP_USER_AGENT"] != null)
    {
        //Create a list of all mobile types
        string[] mobiles = new[]
            {
                "midp", "j2me", "avant", "docomo", "novarra", "palmos", "palmsource", "240x320", "opwv", "chtml",
                "pda", "windows ce", "mmp/", "blackberry", "mib/", "symbian", "wireless", "nokia", "hand", "mobi",
                "phone", "cdm", "up.b", "audio", "SIE-", "SEC-", "samsung", "HTC", "mot-", "mitsu", "sagem", "sony",
                "alcatel", "lg", "eric", "vx", "NEC", "philips", "mmm", "xx", "panasonic", "sharp", "wap", "sch",
                "rover", "pocket", "benq", "java", "pt", "pg", "vox", "amoi", "bird", "compal", "kg", "voda",
                "sany", "kdd", "dbt", "sendo", "sgh", "gradi", "jb", "dddi", "moto", "iphone"
            };

        //Loop through each item in the list created above and check if the header contains that text
        foreach (string s in mobiles)
        {
            if (context.Request.ServerVariables["HTTP_USER_AGENT"].ToLower().Contains(s.ToLower()))
            {
                return true;
            }
        }
    }

    return false;
}

VB.Net

Public Shared Function isMobileBrowser() As Boolean
	'GETS THE CURRENT USER CONTEXT
	Dim context As HttpContext = HttpContext.Current

	'FIRST TRY BUILT IN ASP.NT CHECK
	If context.Request.Browser.IsMobileDevice Then
		Return True
	End If
	'THEN TRY CHECKING FOR THE HTTP_X_WAP_PROFILE HEADER
	If context.Request.ServerVariables("HTTP_X_WAP_PROFILE") IsNot Nothing Then
		Return True
	End If
	'THEN TRY CHECKING THAT HTTP_ACCEPT EXISTS AND CONTAINS WAP
	If context.Request.ServerVariables("HTTP_ACCEPT") IsNot Nothing AndAlso context.Request.ServerVariables("HTTP_ACCEPT").ToLower().Contains("wap") Then
		Return True
	End If
	'AND FINALLY CHECK THE HTTP_USER_AGENT HEADER VARIABLE FOR ANY ONE OF THE FOLLOWING
	If context.Request.ServerVariables("HTTP_USER_AGENT") IsNot Nothing Then
		'Create a list of all mobile types
		Dim mobiles As String() = New () {"midp", "j2me", "avant", "docomo", "novarra", "palmos", _
			"palmsource", "240x320", "opwv", "chtml", "pda", "windows ce", _
			"mmp/", "blackberry", "mib/", "symbian", "wireless", "nokia", _
			"hand", "mobi", "phone", "cdm", "up.b", "audio", _
			"SIE-", "SEC-", "samsung", "HTC", "mot-", "mitsu", _
			"sagem", "sony", "alcatel", "lg", "eric", "vx", _
			"NEC", "philips", "mmm", "xx", "panasonic", "sharp", _
			"wap", "sch", "rover", "pocket", "benq", "java", _
			"pt", "pg", "vox", "amoi", "bird", "compal", _
			"kg", "voda", "sany", "kdd", "dbt", "sendo", _
			"sgh", "gradi", "jb", "dddi", "moto", "iphone"}

		'Loop through each item in the list created above and check if the header contains that text
		For Each s As String In mobiles
			If context.Request.ServerVariables("HTTP_USER_AGENT").ToLower().Contains(s.ToLower()) Then
				Return True
			End If
		Next
	End If

	Return False
End Function