SanSYS

Untitled

Mar 9th, 2015
2,146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.24 KB | None | 0 0
  1. public partial class ViewSwitcher : System.Web.UI.UserControl
  2. {
  3.     protected string CurrentView { get; private set; }
  4.  
  5.     protected string AlternateView { get; private set; }
  6.  
  7.     protected string SwitchUrl { get; private set; }
  8.  
  9.     protected void Page_Load(object sender, EventArgs e)
  10.     {
  11.         // Determine current view
  12.         var isMobile = WebFormsFriendlyUrlResolver.IsMobileView(new HttpContextWrapper(Context));
  13.         CurrentView = isMobile ? "Mobile" : "Desktop";
  14.  
  15.         // Determine alternate view
  16.         AlternateView = isMobile ? "Desktop" : "Mobile";
  17.  
  18.         // Create switch URL from the route, e.g. ~/__FriendlyUrls_SwitchView/Mobile?ReturnUrl=/Page
  19.         var switchViewRouteName = "AspNet.FriendlyUrls.SwitchView";
  20.         var switchViewRoute = RouteTable.Routes[switchViewRouteName];
  21.         if (switchViewRoute == null)
  22.         {
  23.             // Friendly URLs is not enabled or the name of the switch view route is out of sync
  24.             this.Visible = false;
  25.             return;
  26.         }
  27.         var url = GetRouteUrl(switchViewRouteName, new { view = AlternateView, __FriendlyUrls_SwitchViews = true });
  28.         url += "?ReturnUrl=" + HttpUtility.UrlEncode(Request.RawUrl);
  29.         SwitchUrl = url;
  30.     }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment