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

Blog

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" />
</location>

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" />
</location>
<!-- Tablet -->
<location name="Tablet" url="Tablet.aspx" matchExpression="" enabled="true">
  <add property="IsMobile" matchExpression="True" />
  <add property="IsTablet" matchExpression="True" />
</location>
<!-- SmartPhone -->
<location name="SmartPhone" url="SmartPhone.aspx" matchExpression="" enabled="true">
  <add property="IsMobile" matchExpression="True" />
  <add property="IsSmartPhone" matchExpression="True" />
</location>

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" />
</location>

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" />
</location>

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$" />
</location>

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">
 ...
</redirect>

Comments (0)
Mike
>

Products Team

Other posts by Products Team
Contact author

Name:
Email:
Subject:
Message:
x

Tags

.NET 2013 2014 4G 51Degrees 5G A.C.Roma A7 ABI Acer Affiliate Marketing Alcatel Amazon AMP Analysis Analytics Android Android 5.0 Lollipop Android Kitkat Android Lollipop Android Media Stick Apache API Apple Apple TV Archos Asha Asian Market ASP.NET Asus Australia Big Data Black Friday Blackberry Blink Browser C C# Case Study CeBIT CES Chrome Cloud CMS combinations Comparison Competition CoolPad COTW Cron CSS3 Data Data Blog Data File Data Model Daydream Denver Design Desire Eye Desktop Detection Developers Device Device Data Device Detection Device Intelligence Device Popularity Device property Device Types Device Use Display dmexco DoCoMo Doogee DotNetNuke Download Drupal Email EReader E-Reader Ericsson Evaluation Event Examples EXPLAY Rio Facebook feature Firefox Firefox OS Fly Foundation Framework France Galaxy S3 Galaxy S5 Galaxy Tab A Galaxy Tab A 8.0 Galaxy Tab A 9.7 Germany git repositories Global Google Google Daydream GSMA HAProxy Hardware Hisense HTC HTC ONE MAX HTC OS HTML5 HTTP Huawei HUAWEI. UPDATE HUDL Huwaei IBC Icemobile Prime 4.0 IE IFA IIS Image Optimiser Image Optimizer India Infographic Ingeniux Internet usage iOS iOS 7 iOS 8 ipad iPhone iPhone 6 IsEmailBrowser IsWebApp Italy Japan Java Javascript Jolla Kentico Keynote Kindle Kindle Fire Kindle Fire HD Leagoo Lenovo LG Location Log File Analysis LTE Lumia m.dot Map Memory Meta Data Mi 4S Micromax Microsoft Miia Style Mobile Mobile Analysis Mobile Analytics Mobile Devices Mobile Marketing Mixer Module Motorola MVC4 MWC MWC 2017 MWC16 MyPhone Native Native Apps NET New Release News News Letter Nexus Nexus 6 Nexus 9 NFC NGINX Nokia Non-Mobile NVIDIA Omate On7 OnePlus 5 Opera Opera Mini Operating System Optimisation OS OSX 10.10 OTA Panasonic Patent PC Pebble Performance phablet phone PHP Poland Presentation Press Release Price Band PRIV PS4 Python QMobile QR Codes Redirection Research Reseller Responsive Images Responsive web design RESS Review reviews RIM Ringmark RWD Samsung Scala Screen Screen resolution Screen Size SEO Server Server-side optimisation Set Box Set Top Box Sharepoint Shark 1 SHIFT phones Sitecore SLUSH Smart TV Smartphone Smartphones Smartwatches Snapdragon Sony Sony Xperia Spain Swedish Beers Symbian Tablet Tablets Tesco Testing Tips Top 5 TOTW TV UDS UK Umbraco Update updates US User Agent UserAgent User-Agent Vendors Version 3 VoLTE VR Wearable Web Web Apps WebKit WebMatrix White Paper Widgets Widnows WiFi Wiko Wileyfox Windows Windows Phone Xbox XBox One Xiaomi Xperia Xperia z Yosemite Z10 ZenFon 2 ZOPO ZTE