Advertisement
Guest User

Untitled

a guest
Jul 24th, 2014
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.06 KB | None | 0 0
  1. <script type="text/javascript" src="../Scripts/jquery-1.2.6.js"></script>
  2.  
  3. <script type="text/javascript" src="/Scripts/jquery-1.2.6.js"></script>
  4.  
  5. <img src="~/Content/MyImage.jpg">
  6.  
  7. <script type="text/javascript" src="<%=Url.Content("~/Scripts/jquery-1.2.6.js")%>"></script>
  8.  
  9. <%=Html.ScriptInclude("~/Content/Script/jquery.1.2.6.js")%>
  10.  
  11. <a href="@Url.Content("~/Home")">Application home page</a>
  12.  
  13. <a href="~/Home">Application home page</a>
  14.  
  15. <script src="<%=ResolveUrl("~/Scripts/jquery-1.2.6.min.js") %>" type="text/javascript"></script>
  16.  
  17. using System;
  18. using System.IO;
  19. using System.Text;
  20. using System.Text.RegularExpressions;
  21. using System.Web;
  22.  
  23. namespace Demo
  24. {
  25. public class PathRewriter : Stream
  26. {
  27. Stream filter;
  28. HttpContext context;
  29. object writeLock = new object();
  30. StringBuilder sb = new StringBuilder();
  31.  
  32. Regex eofTag = new Regex("</html>", RegexOptions.IgnoreCase | RegexOptions.Compiled);
  33. Regex rootTag = new Regex("/_AppRoot_", RegexOptions.IgnoreCase | RegexOptions.Compiled);
  34.  
  35. public PathRewriter(Stream filter, HttpContext context)
  36. {
  37. this.filter = filter;
  38. this.context = context;
  39. }
  40.  
  41. public override void Write(byte[] buffer, int offset, int count)
  42. {
  43. string temp;
  44.  
  45. lock (writeLock)
  46. {
  47. temp = Encoding.UTF8.GetString(buffer, offset, count);
  48. sb.Append(temp);
  49.  
  50. if (eofTag.IsMatch(temp))
  51. RewritePaths();
  52. }
  53. }
  54.  
  55. public void RewritePaths()
  56. {
  57. byte[] buffer;
  58. string temp;
  59. string root;
  60.  
  61. temp = sb.ToString();
  62. root = context.Request.ApplicationPath;
  63. if (root == "/") root = "";
  64.  
  65. temp = rootTag.Replace(temp, root);
  66. buffer = Encoding.UTF8.GetBytes(temp);
  67. filter.Write(buffer, 0, buffer.Length);
  68. }
  69.  
  70. public override bool CanRead
  71. {
  72. get { return true; }
  73. }
  74.  
  75. public override bool CanSeek
  76. {
  77. get { return filter.CanSeek; }
  78. }
  79.  
  80. public override bool CanWrite
  81. {
  82. get { return true; }
  83. }
  84.  
  85. public override void Flush()
  86. {
  87. return;
  88. }
  89.  
  90. public override long Length
  91. {
  92. get { return Encoding.UTF8.GetBytes(sb.ToString()).Length; }
  93. }
  94.  
  95. public override long Position
  96. {
  97. get { return filter.Position; }
  98. set { filter.Position = value; }
  99. }
  100.  
  101. public override int Read(byte[] buffer, int offset, int count)
  102. {
  103. return filter.Read(buffer, offset, count);
  104. }
  105.  
  106. public override long Seek(long offset, SeekOrigin origin)
  107. {
  108. return filter.Seek(offset, origin);
  109. }
  110.  
  111. public override void SetLength(long value)
  112. {
  113. throw new NotImplementedException();
  114. }
  115. }
  116.  
  117. public class PathFilterModule : IHttpModule
  118. {
  119. public void Dispose()
  120. {
  121. return;
  122. }
  123.  
  124. public void Init(HttpApplication context)
  125. {
  126. context.ReleaseRequestState += new EventHandler(context_ReleaseRequestState);
  127. }
  128.  
  129. void context_ReleaseRequestState(object sender, EventArgs e)
  130. {
  131. HttpApplication app = sender as HttpApplication;
  132. if (app.Response.ContentType == "text/html")
  133. app.Response.Filter = new PathRewriter(app.Response.Filter, app.Context);
  134. }
  135. }
  136. }
  137.  
  138. <a href="@Url.Content("~/Home")">Application home page</a>
  139.  
  140. protected void Application_BeginRequest(object sender, EventArgs e)
  141. {
  142. Request.ServerVariables.Remove("IIS_WasUrlRewritten");
  143. }
  144.  
  145. <script type="text/javascript" src="/MyProject/Scripts/jquery-1.2.6.js"></script>
  146.  
  147. <a href=@Helper.Root()/about">About Us</a>
  148.  
  149. public static string Root()
  150. {
  151. if (HttpContext.Current.Request.Url.Host == "localhost")
  152. {
  153. return "";
  154. }
  155. else
  156. {
  157. return "/productionroot";
  158. }
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement