Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2018
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. /// <summary>
  2. /// Redirects to the mobile view if on a supported device
  3. /// </summary>
  4. [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = false)]
  5. public class MobileRedirectAttribute : AuthorizeAttribute
  6. {
  7. private string _clientFragment;
  8.  
  9. /// <summary>
  10. /// Default Constructor
  11. /// </summary>
  12. public MobileRedirectAttribute()
  13. {
  14. _clientFragment = string.Empty;
  15. }
  16.  
  17. /// <summary>
  18. /// Constructor that takes an argument
  19. /// </summary>
  20. /// <param name="clientUrl">The url fragment we should append to the url</param>
  21. public MobileRedirectAttribute(string clientFragment)
  22. {
  23. _clientFragment = clientFragment;
  24. }
  25.  
  26. /// <summary>
  27. /// Tests if this request originates from a supported mobile device
  28. /// and redirects as appropriate
  29. /// </summary>
  30. /// <param name="ctx">The action execution context</param>
  31. public override void OnAuthorization(AuthorizationContext ctx)
  32. {
  33. if (ctx.HttpContext.Request.Browser.IsMobileDevice)
  34. {
  35. // parse the fragment with request parameters
  36. string fragment = ParseClientFragment(ctx);
  37.  
  38. // construct the redirect url
  39. UrlHelper urlHelper = new UrlHelper(ctx.RequestContext);
  40. string url = string.Format("{0}#{1}", urlHelper.Action("Index", "Mobile"), fragment);
  41.  
  42. // return redirect result to prevent action execution
  43. ctx.Result = new RedirectResult(url);
  44. }
  45. }
  46.  
  47. /// <summary>
  48. /// Parses the client fragment and replaces :[token] with the request parameter
  49. /// </summary>
  50. /// <param name="ctx">The controller context</param>
  51. /// <returns>The parsed fragment</returns>
  52. private string ParseClientFragment(ControllerContext ctx)
  53. {
  54. string parsedFragment = _clientFragment ?? string.Empty;
  55.  
  56. if (!string.IsNullOrEmpty(parsedFragment))
  57. {
  58. NameValueCollection @params = ctx.HttpContext.Request.Params;
  59. MatchCollection matches = Regex.Matches(_clientFragment, ":[a-zA-Z]+");
  60. RouteData routeData = RouteTable.Routes.GetRouteData(ctx.HttpContext);
  61.  
  62. // check each token and replace with param or route values
  63. foreach (Match match in matches)
  64. {
  65. string token = match.Value.TrimStart(':');
  66. string value = @params[token];
  67.  
  68. // if we haven;t got a parameter here we must check the route values
  69. if (string.IsNullOrEmpty(value) && routeData.Values.ContainsKey(token))
  70. {
  71. value = routeData.Values[token] as string;
  72. }
  73.  
  74. // perform the replace
  75. parsedFragment = parsedFragment.Replace(match.Value, value);
  76. }
  77. }
  78.  
  79. return parsedFragment;
  80. }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement