51DegreesTM

Redirection in .NET

Engineering

10/24/2014 12:23 PM

Device Detection .NET Development

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>