3

Can anybody suggest me which is the better place to verify mobile browser and redirect to mobile site.

I am thinking of using DetectMobileBrowsers to verify mobile browsers.

And I am thinking of doing this in Application_Start or Session_Start. Please suggest me which is the better place to do the same.

This is my Session_Start block

Protected Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
    ' Fires when the session is started
    'Dim request As HttpRequest = HttpContext.Current.Request
    'If request.Browser.IsMobileDevice Then
    '    Response.Redirect("http://localhost:26270/Default.aspx")
    'End If
    Response.Redirect("http://google.com")
End Sub

Thanks

Naresh
  • 2,667
  • 13
  • 44
  • 69
  • Definitely better to do this on the server, rather than client, so you could probably remove `javascript` and `jquery`, but are you using WebForms or MVC? MVC actually makes it quite easy to offer a different view for mobile platforms. – MrOBrian Sep 21 '12 at 18:44

3 Answers3

4

Instead of using Response.Redirect("http://m.yoursite.com") it is better to use 2 line wich will temporarily redirect you to the mobile version. It's better to use:

Response.Status="302 Moved Temporarily"
Response.AddHeader "Location","http://m.yoursite.com"
johannes
  • 7,262
  • 5
  • 38
  • 57
Adrian
  • 41
  • 1
3

You should probably have it in Session_Start as the device will remain the same once a session is established which will be called the triggered when a user accesses the site for the first time.

just google and read about it..

FYI, Application_Start is called once for the lifetime of the application domain and Session_Start event is raised each time a new session is created.

Reading about application life cycle will help you better understand all these events.

Baz1nga
  • 15,485
  • 3
  • 35
  • 61
  • Is Session_Start will be called when application loaded for first time or user logged into the application when or session object used for first time. I have Session_start in my global.asax but it is not being invoked when my application loads for first time. – Naresh Sep 21 '12 at 19:19
  • when application loads for the first time session_start will not be called.. session_start is called when a user accesses your site for the first time.. its got nothing to do with accessing the Session object. – Baz1nga Sep 21 '12 at 20:41
  • I am not sure why Session_Start is not being invoked in my application. I have breakpoint in my Session_start but this event is not invoking. – Naresh Sep 21 '12 at 20:53
  • does this help: http://stackoverflow.com/questions/7660739/global-asax-breakpoint-not-hit – Baz1nga Sep 21 '12 at 20:55
  • Not really...I can debug the other events like Application_Start, Application_BeginRequest but not Session_Start – Naresh Sep 21 '12 at 21:04
  • forget the breakpoint, just have some logging statements in there and see if you can see them.. – Baz1nga Sep 21 '12 at 21:08
  • No luck....I have tried to redirect to goolge from Session_Start. It's not redirecting to google. – Naresh Sep 21 '12 at 21:23
  • are you making sure that you are a new user? open an incognito window in Chrome and then try! – Baz1nga Sep 21 '12 at 21:24
  • Tried..but no luck. I have updated my query with Session_Start block. – Naresh Sep 21 '12 at 21:31
2

Consider Application_BeginRequest. Be sure to check the request Url is the page you are requesting, so that you are not running the check for each static file that is requested.

void Application_BeginRequest(object sender, EventArgs e)
{
   var u = Request.ServerVariables("HTTP_USER_AGENT");
   var uri =  Request.Url.AbsoluteUri.ToLower();
   if (url.Contains(".aspx"))
   {
      //put DetectMobileBrowsersCode Here

      if (b.IsMatch(u) || v.IsMatch(Left(u, 4)))
      {
          Response.Redirect("http://m.yoursite.com");
      } 
   }   
}
James Lawruk
  • 30,112
  • 19
  • 130
  • 137
  • I initially thought the same, but the thing is that once the user accesses the site it doesnt make sense to keep redirecting all his requests to m.yoursite.com or keep checking what his user_agent is... it probably makes sense to check it once and that is where session_start comes into the picture.. – Baz1nga Sep 21 '12 at 20:42
  • @Baz1nga Yeah, this might not be optimal. But with just a session_start check, what if a mobile user that was already redirected ends up requesting a page from the main site and the session is still active. The session_start will not execute, and the user will not be redirected. – James Lawruk Sep 22 '12 at 01:38
  • you need to think of an alternative method in that case, but I think handling it in aoplication_beginrequest is not optimum since its called for all requested resources – Baz1nga Sep 22 '12 at 04:20
  • but if the user does smthing like that, he means he wants the normal resource and if we now serve the mobile version it might be against his wish – Baz1nga Sep 22 '12 at 04:22