ViewSwitcher.ascx.cs.pp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Routing;
  6. using System.Web.UI;
  7. using System.Web.UI.WebControls;
  8. using Microsoft.AspNet.FriendlyUrls.Resolvers;
  9. namespace $rootnamespace$
  10. {
  11. public partial class ViewSwitcher : System.Web.UI.UserControl
  12. {
  13. protected string CurrentView { get; private set; }
  14. protected string AlternateView { get; private set; }
  15. protected string SwitchUrl { get; private set; }
  16. protected void Page_Load(object sender, EventArgs e)
  17. {
  18. // Determine current view
  19. var isMobile = WebFormsFriendlyUrlResolver.IsMobileView(new HttpContextWrapper(Context));
  20. CurrentView = isMobile ? "Mobile" : "Desktop";
  21. // Determine alternate view
  22. AlternateView = isMobile ? "Desktop" : "Mobile";
  23. // Create switch URL from the route, e.g. ~/__FriendlyUrls_SwitchView/Mobile?ReturnUrl=/Page
  24. var switchViewRouteName = "AspNet.FriendlyUrls.SwitchView";
  25. var switchViewRoute = RouteTable.Routes[switchViewRouteName];
  26. if (switchViewRoute == null)
  27. {
  28. // Friendly URLs is not enabled or the name of the swith view route is out of sync
  29. this.Visible = false;
  30. return;
  31. }
  32. var url = GetRouteUrl(switchViewRouteName, new { view = AlternateView });
  33. url += "?ReturnUrl=" + HttpUtility.UrlEncode(Request.RawUrl);
  34. SwitchUrl = url;
  35. }
  36. }
  37. }