RewriterUtils.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using System;
  2. using System.Web;
  3. namespace URLRewriter
  4. {
  5. /// <summary>
  6. /// Provides utility helper methods for the rewriting HttpModule and HttpHandler.
  7. /// </summary>
  8. /// <remarks>This class is marked as internal, meaning only classes in the same assembly will be
  9. /// able to access its methods.</remarks>
  10. internal class RewriterUtils
  11. {
  12. #region RewriteUrl
  13. /// <summary>
  14. /// Rewrite's a URL using <b>HttpContext.RewriteUrl()</b>.
  15. /// </summary>
  16. /// <param name="context">The HttpContext object to rewrite the URL to.</param>
  17. /// <param name="sendToUrl">The URL to rewrite to.</param>
  18. internal static void RewriteUrl(HttpContext context, string sendToUrl)
  19. {
  20. string x, y;
  21. RewriteUrl(context, sendToUrl, out x, out y);
  22. }
  23. /// <summary>
  24. /// Rewrite's a URL using <b>HttpContext.RewriteUrl()</b>.
  25. /// </summary>
  26. /// <param name="context">The HttpContext object to rewrite the URL to.</param>
  27. /// <param name="sendToUrl">The URL to rewrite to.</param>
  28. /// <param name="sendToUrlLessQString">Returns the value of sendToUrl stripped of the querystring.</param>
  29. /// <param name="filePath">Returns the physical file path to the requested page.</param>
  30. internal static void RewriteUrl(HttpContext context, string sendToUrl, out string sendToUrlLessQString, out string filePath)
  31. {
  32. // see if we need to add any extra querystring information
  33. if (context.Request.QueryString.Count > 0)
  34. {
  35. if (sendToUrl.IndexOf('?') != -1)
  36. sendToUrl += "&" + context.Request.QueryString.ToString();
  37. else
  38. sendToUrl += "?" + context.Request.QueryString.ToString();
  39. }
  40. // first strip the querystring, if any
  41. string queryString = String.Empty;
  42. sendToUrlLessQString = sendToUrl;
  43. if (sendToUrl.IndexOf('?') > 0)
  44. {
  45. sendToUrlLessQString = sendToUrl.Substring(0, sendToUrl.IndexOf('?'));
  46. queryString = sendToUrl.Substring(sendToUrl.IndexOf('?') + 1);
  47. }
  48. // grab the file's physical path
  49. filePath = string.Empty;
  50. filePath = context.Server.MapPath(sendToUrlLessQString);
  51. // rewrite the path...
  52. context.RewritePath(sendToUrlLessQString, String.Empty, queryString);
  53. // NOTE! The above RewritePath() overload is only supported in the .NET Framework 1.1
  54. // If you are using .NET Framework 1.0, use the below form instead:
  55. // context.RewritePath(sendToUrl);
  56. }
  57. #endregion
  58. /// <summary>
  59. /// Converts a URL into one that is usable on the requesting client.
  60. /// </summary>
  61. /// <remarks>Converts ~ to the requesting application path. Mimics the behavior of the
  62. /// <b>Control.ResolveUrl()</b> method, which is often used by control developers.</remarks>
  63. /// <param name="appPath">The application path.</param>
  64. /// <param name="url">The URL, which might contain ~.</param>
  65. /// <returns>A resolved URL. If the input parameter <b>url</b> contains ~, it is replaced with the
  66. /// value of the <b>appPath</b> parameter.</returns>
  67. internal static string ResolveUrl(string appPath, string url)
  68. {
  69. if (url.Length == 0 || url[0] != '~')
  70. return url; // there is no ~ in the first character position, just return the url
  71. else
  72. {
  73. if (url.Length == 1)
  74. return appPath; // there is just the ~ in the URL, return the appPath
  75. if (url[1] == '/' || url[1] == '\\')
  76. {
  77. // url looks like ~/ or ~\
  78. if (appPath.Length > 1)
  79. return appPath + "/" + url.Substring(2);
  80. else
  81. return "/" + url.Substring(2);
  82. }
  83. else
  84. {
  85. // url looks like ~something
  86. if (appPath.Length > 1)
  87. return appPath + "/" + url.Substring(1);
  88. else
  89. return appPath + url.Substring(1);
  90. }
  91. }
  92. }
  93. }
  94. }