Rewriter 伪静态 提交 地址变回动态地址

首页 | 新闻 | 交流 | 问吧 | 文档 | 手册 | 下载 | 博客

Rewriter 伪静态 提交 地址变回动态地址

作者:  时间: 2011-03-30

using System;
using System.Web;
using System.Web.UI;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Configuration;

namespace BLL
{
    /// <summary>
    /// PageBase 的摘要说明。
    /// </summary>
    public class PageBase : Page
    {
        private static object obj = new object();
        //// <summary>
        ///  重写默认的HtmlTextWriter方法,修改form标记中的value属性,使其值为重写的URL而不是真实URL。www.SvnHost.cn
        /// </summary>
        /// <param name="writer"></param>
        protected override void Render(HtmlTextWriter writer)
        {
            if (writer is System.Web.UI.Html32TextWriter)
            {
                writer = new FormFixerHtml32TextWriter(writer.InnerWriter);
            }
            else
            {
                writer = new FormFixerHtmlTextWriter(writer.InnerWriter);
            }

            base.Render(writer);
        }
    }

    public class FormFixerHtml32TextWriter : System.Web.UI.Html32TextWriter
    {
        public FormFixerHtml32TextWriter(TextWriter writer)
            : base(writer)
        {
        }

        public override void WriteAttribute(string name, string value, bool encode)
        {
            // 如果当前输出的属性为form标记的action属性,则将其值替换为重写后的虚假URL
            if (string.Compare(name, "action", true) == 0)
            {
                value = HttpContext.Current.Request.RawUrl;
            }
            base.WriteAttribute(name, value, encode);
        }
    }


    public class FormFixerHtmlTextWriter : System.Web.UI.HtmlTextWriter
    {
        public FormFixerHtmlTextWriter(TextWriter writer)
            : base(writer)
        {
        }

        public override void WriteAttribute(string name, string value, bool encode)
        {
            if (string.Compare(name, "action", true) == 0)
            {
                value = HttpContext.Current.Request.RawUrl;
            }

            base.WriteAttribute(name, value, encode);
        }
    }
}