1

In a nutshell, I'm trying to figure out how to programmatically enable and disable the redirection for mobile phones, based on a session value, on a per request basis (not statically for everyone).

Here's the back story:

I'm currently using 51Degrees in my ASP.NET application to redirect requests to the mobile version of the site. I would like to add a feature where users can enable and disable this redirection from a "Settings" page. The setting will be different for each user, and so far every setting I've found to disable 51Degrees is static. This makes it difficult to disable it for a subset of users.

One semi-solution I had was to set firstRequestOnly="true" and reverse the redirect if it wasn't supposed to happen. Although firstRequestOnly="true" has caused a number of other headaches (unrelated) so I would like to keep it at firstRequestOnly="false" and simply enable/disable the redirection based on a session value per request, or per user. The problem is that I can't figure out a clean way to do this.

Adam
  • 3,063
  • 5
  • 35
  • 49

2 Answers2

0

Set the cookie in the page prerender event based on the setting your user has choosen in their profile. You would need to have firstRequestOnly set to true. If the user does not want redirection it should have a very long expiry time, if they do then set a short expiry time.

James Rosewell
  • 516
  • 3
  • 6
-1

I ended up just downloading the source code and adding in an option to opt-out of the redirect if a certain session field was present and set to true. This allowed me to set that session value from my code depending on what the user had setup in the settings section. Not the best solution since it's going to be more difficult to upgrade 51Degrees down the road, but it works.

Adam
  • 3,063
  • 5
  • 35
  • 49
  • Did you try setting a cookie in the response with the name "51D"? This is one of the methods 51Degrees.mobi uses to indicate the browser has accessed the web site before. If the user didn't want to be redirected you would ensure this cookie was set and had a very long expiry time in the prerender event of the page. Alternatively if sessions are enabled the "51D_Expiry_Time" is used to indicate previous requests. Again you could set this in your code depending on the users preferences. For more information have a look at AlreadyAccessedCookieName and ExpiryTime constants and where they're used. – James Rosewell Feb 10 '12 at 09:57
  • I did try that actually. I noticed for that to work you need to set firstRequestOnly=true and clear that cookie if the redirect wasn't desired, followed by a redirect to the intended page. Although this meant that subsequent requests would end up following the same algorithm since they would again be considered "first requests" after the cookie was cleared. This got messy when post data got involved, or when we wanted to do our own redirect for something else. I might be misunderstanding though. Was that the algorithm you were talking about or am I thinking of something else? – Adam Feb 15 '12 at 22:42
  • 1
    Can't you set the cookie in the page prerender event based on the setting your user has choosen in their profile? You would need to have firstRequestOnly set to true. If the user does not want redirection it should have a very long expiry time, if they do then set a short expiry time. Alternatively post the changes you've made and we'll see about integrating into a future version of the core. – James Rosewell Feb 22 '12 at 09:56
  • Thanks! That worked. If you wanted to put that as an answer I'll mark it as the accepted answer. – Adam Feb 24 '12 at 22:15
  • Thank you. Here's the answer. – James Rosewell Mar 12 '12 at 22:51