url重写以后保持postback地址

让所有页面集成一下类即可实现url重写以后保持postback地址

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。http://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);
        }
    }
}

你可能感兴趣的:(url重写)