也談UpdatePanel與UrlRewrite一起work時出現Form Action屬性的問題

來源:互聯網
上載者:User
首先感謝老趙寫了一篇文章,http://www.cnblogs.com/JeffreyZhao/archive/2006/12/27/604373.aspx#post

其實出現這個問題,根本不是MS Ajax的失誤,完全是我們沒有用好URLRewrite這個東西的原因。

老趙的解決方案是重寫了一個Form類,把原來的Form的Action給清空了。

能否正常工作我不知道,但是我認為“清空”,“利用預設屬性”這樣一類的做法是很危險的。~~~

重寫Form類的,引用也有點麻煩,我覺的重寫一個Page,比較方便。我在www.365rss.cn中的做法如下:

using System;
using System.IO;
using System.Web;
using System.Web.UI;
namespace okpower.Utility
{
    /**//// <summary>
    /// URLRewrite 頁面基類
    /// 作者:Kai.Ma http://kaima.cnblogs.com
    /// </summary>
    public class URLRewritePage : Page
    {
        public URLRewritePage()
        {
        }

        protected override void Render(HtmlTextWriter writer)
        {            
            writer = new FormFixerHtmlTextWriter(writer.InnerWriter);
            base.Render(writer);
        }
    }

    internal class FormFixerHtmlTextWriter : System.Web.UI.HtmlTextWriter
    {
        private string _url;
        internal FormFixerHtmlTextWriter(TextWriter writer)
            : base(writer)
        {
            _url = HttpContext.Current.Request.RawUrl;
        }

        public override void WriteAttribute(string name, string value, bool encode)
        {
            // 如果當前輸出的屬性為form標記的action屬性,則將其值替換為重寫後的虛假URL
            if (_url != null && string.Compare(name, "action", true) == 0)
            {
                value = _url;
            }

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

}

以後繼承這個URLRewritePage就可以了,甚至可以進web.config設定,一勞永逸。
歡迎交流

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.