RewriterForm.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. using System.IO;
  2. using System.Web;
  3. using System.Web.UI;
  4. using System.Web.UI.Adapters;
  5. /// <summary>
  6. /// FormRewriter 的摘要说明
  7. /// </summary>
  8. namespace SiteCore.URLRewriter
  9. {
  10. public class FormRewriterControlAdapter : ControlAdapter
  11. {
  12. protected override void Render(HtmlTextWriter writer)
  13. {
  14. base.Render(new RewriteFormHtmlTextWriter(writer));
  15. }
  16. }
  17. public class RewriteFormHtmlTextWriter : HtmlTextWriter
  18. {
  19. public RewriteFormHtmlTextWriter(HtmlTextWriter writer)
  20. : base(writer)
  21. {
  22. base.InnerWriter = writer.InnerWriter;
  23. }
  24. public RewriteFormHtmlTextWriter(TextWriter writer)
  25. : base(writer)
  26. {
  27. base.InnerWriter = writer;
  28. }
  29. public override void WriteAttribute(string name, string value, bool fEncode)
  30. {
  31. //If the attribute we are writing is the "action" attribute, and we are not on a sub-control,
  32. //then replace the value to write with the raw URL of the request - which ensures that we'll
  33. //preserve the PathInfo value on postback scenarios
  34. if (name == "action")
  35. {
  36. HttpContext context = HttpContext.Current;
  37. if (context.Items["ActionAlreadyWritten"] == null)
  38. {
  39. //We will use the Request.RawUrl property within ASP.NET to retrieve the origional
  40. //URL before it was re-written.
  41. value = context.Request.RawUrl;
  42. //Indicate that we've already rewritten the <form>'s action attribute to prevent
  43. //us from rewriting a sub-control under the <form> control
  44. context.Items["ActionAlreadyWritten"] = true;
  45. }
  46. }
  47. base.WriteAttribute(name, value, fEncode);
  48. }
  49. }
  50. }