Mobile Web Browser Detection and Redirection in C# for ASP.net (server-side)
Adding server-side browser detection and redirection to your website using the ASP.net platform and C# (C-Sharp) is actually quite easy. Below, you will see the code which we have implemented on our site. Though it may not catch ALL browsers (because there are a heck of a lot of them), it will catch most.
This code should be inserted into the Page_Load event of the codebehind file (e.g., samplepage.aspx.cs). To enable this site-wide, just add it to the Page_Load event of the Master Page file.
if (Request.Headers["User-Agent"] != null && (Request.Browser["IsMobileDevice"] == "true" || Request.Browser["BlackBerry"] == "true" || Request.UserAgent.ToUpper().Contains("MIDP") || Request.UserAgent.ToUpper().Contains("CLDC")) || Request.UserAgent.ToLower().Contains("iphone"))
{
Response.Redirect("http://mobile.abwebsitedesign.com");
}
The preceding code will allow you to add as many User Agents as you wish by just adding additional Request.UserAgent properties. The else statement is not necessary in this case, because we want the page to load normally, unless the UserAgent is not a standard browser.
If you wanted to redirect to a certain page for either case, you can do something similar to the following as well:
if (Request.Headers["User-Agent"] != null && (Request.Browser["IsMobileDevice"] == "true" || Request.Browser["BlackBerry"] == "true" || Request.UserAgent.ToUpper().Contains("MIDP") || Request.UserAgent.ToUpper().Contains("CLDC")) || Request.UserAgent.ToLower().Contains("iphone"))
{
Response.Redirect("http://www.abwebsitedesign.com/mobile.html");
}
else
{
Response.Redirect("http://www.abwebsitedesign.com/standard.aspx");
}
This code should be inserted into the Page_Load event of the codebehind file (e.g., samplepage.aspx.cs). To enable this site-wide, just add it to the Page_Load event of the Master Page file.
if (Request.Headers["User-Agent"] != null && (Request.Browser["IsMobileDevice"] == "true" || Request.Browser["BlackBerry"] == "true" || Request.UserAgent.ToUpper().Contains("MIDP") || Request.UserAgent.ToUpper().Contains("CLDC")) || Request.UserAgent.ToLower().Contains("iphone"))
{
Response.Redirect("http://mobile.abwebsitedesign.com");
}
The preceding code will allow you to add as many User Agents as you wish by just adding additional Request.UserAgent properties. The else statement is not necessary in this case, because we want the page to load normally, unless the UserAgent is not a standard browser.
If you wanted to redirect to a certain page for either case, you can do something similar to the following as well:
if (Request.Headers["User-Agent"] != null && (Request.Browser["IsMobileDevice"] == "true" || Request.Browser["BlackBerry"] == "true" || Request.UserAgent.ToUpper().Contains("MIDP") || Request.UserAgent.ToUpper().Contains("CLDC")) || Request.UserAgent.ToLower().Contains("iphone"))
{
Response.Redirect("http://www.abwebsitedesign.com/mobile.html");
}
else
{
Response.Redirect("http://www.abwebsitedesign.com/standard.aspx");
}
Labels: ASP.net, Mobile Web, Programming

5 Comments:
I've been searching all the web for a solution for this... many of them was old and dont work in the new devices, and your script was the final solution!!! thanx!
By
julian, At
November 27, 2008 1:58 PM
Just what I've been looking for! I also added:
Request.UserAgent.ToUpper().Contains("BREW"))
for some sprint and other CDMA/BREW phones.
By
Anonymous, At
December 19, 2008 4:55 PM
I've also been looking for some code to do this for a while now!
Excellent. Just tested it across multiple browsers by people in our office. Works on iphone, opera mini, iris, and pc still works great!!
Very pleased!
By
Del, At
June 19, 2009 7:25 AM
You're welcome! Glad to hear you are finding it useful.
By
Chris, At
June 19, 2009 2:38 PM
This post has been removed by a blog administrator.
By
Web Designing India, At
June 24, 2009 2:09 AM
Post a Comment
Links to this post:
Create a Link
<< Home