• About Us
  • Blog
  • Basket
  • Account
  • Sign In


Redirection in .NET

Published on Friday, October 24, 2014

Redirection in .NET

Not only for mobiles

51Degrees redirection module is a valid alternative to incorporating detection logic in to your .NET pages. This is a very flexible approach that can make use of all device properties available to your data file and you can easily create and maintain a set of rules for any eventuality. Maintenance is also less of an issue as with a change in requirements only one file needs to be changed as opposed to changing every occurrence of detection logic in the application.

All of the properties available in your data file can be used with the redirection module. The module and all rules are configured in the section of 51Degrees.config file.

This is the most basic rule which redirects all devices that are detected as mobile to Default.aspx page in the Mobile folder.

<location name="Mobile" url="~/Mobile/Default.aspx" matchExpression="" enabled="true">
 <add property="IsMobile" matchExpression="True" enabled="true" />

Each <location> element redirects to a specific location based on one or more rules. <location> element has 3 properties:

  • name: is a mandatory unique identifier for the location. Can also be used in the log file for debugging purposes.
  • url: is mandatory that tells detector the name and location of the page device should be redirected to.
  • matchExpression: an optional property that can take the request URL and match segments to be used in place of numeric parameters contained within the URL attribute between {}.

Each location must contain at least one filter property (add). All added filter properties must evaluate to True in order for the redirect to take place.

In this example Desktops, Tablets and SmartPhones get redirected to the relevant pages.

<!-- Desktop -->
<location name="Desktop" url="Desktop.aspx" matchExpression="" enabled="true">
  <add property="IsMobile" matchExpression="False" />
<!-- Tablet -->
<location name="Tablet" url="Tablet.aspx" matchExpression="" enabled="true">
  <add property="IsMobile" matchExpression="True" />
  <add property="IsTablet" matchExpression="True" />
<!-- SmartPhone -->
<location name="SmartPhone" url="SmartPhone.aspx" matchExpression="" enabled="true">
  <add property="IsMobile" matchExpression="True" />
  <add property="IsSmartPhone" matchExpression="True" />

In some cases you may wish to access the page ignoring the redirection rules. The following example rule allows you to access the desired page without being redirected by adding the ?noredirect or &noredirect to the url.

<location name="noredirect" url="" matchExpression="" enabled="true">
 <add property="Url" matchExpression="[&amp;|\?]noredirect" enabled="true" />

The following example uses ScreenPixelsWidth property to redirect devices with screen sizes between 0 and 999 pixels to a page called medium.aspx. Using this template you can set up a more complex redirections based on the screen size ranges.

<location name="medium" url="medium.aspx" matchExpression="" enabled="true">
  <add property="ScreenPixelsWidth" matchExpression="^([0-9]|[0-9][0-9]|[0-9][0-9][0-9])$" enabled="true" />

If you want to redirect specific page/pages you can make use of the origUrl property as shown below. Using this property tells the redirect module that redirect will only happen for a given page. In this example visitors will be redirected to MobileWelcome.aspx page when they visit https://yourwebsite.com. This rule won't work on any other page.

<location name="MobileWelcome" url="MobileWelcome.aspx" matchExpression="" enabled="true">
  <add property="IsMobile" matchExpression="True" />
  <add property="origUrl" matchExpression="^http://yoursite.com$" />

In order to allow your mobile visitors to view the full version of the website you can make use of the firstRequestOnly property which, if set to True will only redirect to the mobile version of the website upon the first request. Setting it to False will effectively force users on mobile devices to view the mobile version of the website. This property should be accompanied by the devicesFile property which is used to store details of devices that made a request before. This is not device data file used for detection.

<redirect devicesFile="~/App_Data/MobileVisitors.dat" firstRequestOnly="true">

Comments (0)

Products Team

Other posts by Products Team
Contact author



.NET 4G 51Degrees 5G Acer Adform Adtech Advertising Afilias Alcatel Amazon AMP Analysis Analytics Android Apache API Apple Asian Market ASP.NET Asus Blackberry Browser C C# Centro Chrome chromium Client hints Cloud CMS CPU CSS3 Data Data Blog Data File Daydream Design Detection Developers Device Device Data Device Detection Device Intelligence Device Models Device property DeviceAtlas Disney dmexco DotNetNuke Download ebay Ericsson Event Facebook Firefox Foundation Framework Galaxy git repositories Google Google Analytics Google Daydream GPU GSMA Guess HAProxy Hash Trie HTC HTML5 HTTP HTTP Headers Huawei Infinix Ingeniux Internet usage iOS iOS 13 ipad iPadOS iPhone iPhone 11 Java Javascript Kentico LG Liferay LTE m.dot Memory Memory leak Meta Data Microsoft Mobile Mobile Analysis Mobile Analytics Mobile Devices Mobile Marketing Mixer Motorola Mozilla MWC MWC 2017 MWC16 Native Apps NET New Release News Nexus NFC NGINX Nokia OnePlus 5 Opera Operating System Oppo Optimisation OS Patent Performance PHP Press Release Price Band programmatic Publishers Python Redirection Research Responsive Images Responsive web design RESS Review RTB RWD Safari Samsung Scala ScientiaMobile SEO Server Server-side optimisation Seznam.cz Sitecore Smart TV Smartphone Smartwatches Sony Swedish Beers Tablet Tencent Testing Tips Tutorial Umbraco Update User Agent User-Agent Valgrind Varnish Varnish Cache Video Vodafone VoLTE Web Web Apps Web content management Webtrekk White Paper Widgets WiFi Windows WURFL Xiaomi Xperia ZTE