urlrewrite 保持 posturl
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
1 using System;
2 using System.Web;
3 using System.Web.UI;
4 using System.IO;
5 using System.Text;
6 using System.Text.RegularExpressions;
7 using System.Configuration;
8
9 namespace BLL
10 {
11 /// <summary>
12 /// PageBase 的摘要说明。
13 /// </summary>
14 public class PageBase : Page
15 {
16 private static object obj = new object();
17 //// <summary>
18 /// 重写默认的HtmlTextWriter方法,修改form标记中的value属性,使其值为重写的URL而不是真实URL。
19 /// </summary>
20 /// <param name="writer"></param>
21 protected override void Render(HtmlTextWriter writer)
22 {
23 if (writer is System.Web.UI.Html32TextWriter)
24 {
25 writer = new FormFixerHtml32TextWriter(writer.InnerWriter);
26 }
27 else
28 {
29 writer = new FormFixerHtmlTextWriter(writer.InnerWriter);
30 }
31 base.Render(writer);
32 }
33 }
34 public class FormFixerHtml32TextWriter : System.Web.UI.Html32TextWriter
35 {
36 public FormFixerHtml32TextWriter(TextWriter writer) : base(writer)
37 {
38
39 }
40 public override void WriteAttribute(string name, string value, bool encode)
41 {
42 // 如果当前输出的属性为form标记的action属性,则将其值替换为重写后的虚假URL
43 if (string.Compare(name, "action", true) == 0)
44 {
45 value = HttpContext.Current.Request.RawUrl;
46 }
47 base.WriteAttribute(name, value, encode);
48 }
49
50 }
51 public class FormFixerHtmlTextWriter : System.Web.UI.HtmlTextWriter
52 {
53
54 public FormFixerHtmlTextWriter(TextWriter writer) : base(writer)
55 {
56
57 }
58 public override void WriteAttribute(string name, string value, bool encode)
59 {
60 if (string.Compare(name, "action", true) == 0)
61 {
62 value = HttpContext.Current.Request.RawUrl;
63 }
64 base.WriteAttribute(name, value, encode);
65
66 }
67
68 }
69
70 }