Custom Search
Logiclabz
  • Home
  • Asp.Net
  • Change Asp.Net Web Server Form action url with Response.Filter

Change Asp.Net Web Server Form action url with Response.Filter

Generally the asp.net server form action property point to current url. This can be changed using the below class which inherited Stream. This new user-defined class "clsResponseModifier" gets the form tag through regex match and replace the action url that we specify. First create this class "clsResponseModifier"

public class clsResponseModifier : Stream
{
    private Stream _sink;

    private long _position;

    string _url;

    public override bool CanRead { get { return true; } }

    public override bool CanSeek { get { return true; } }

    public override bool CanWrite { get { return true; } }

    public override long Length { get { return 0; } }

    public override long Position { get { return _position; } set { _position = value; } }

    public override void SetLength(long length) { _sink.SetLength(length); }

    public override void Close() { _sink.Close(); }

    public override void Flush() { _sink.Flush(); }

    public clsResponseModifier(Stream sink, string url) 
	{ _sink = sink; _url = "$1" + url + "$3"; }

    public override long Seek(long offset, System.IO.SeekOrigin direction) 
	{ return _sink.Seek(offset, direction); }

    public override int Read(byte[] buffer, int offset, int count) 
	{ return _sink.Read(buffer, offset, count); }

    public override void Write(byte[] buffer, int offset, int count)
    {
        string s = System.Text.UTF8Encoding.UTF8.GetString(buffer, offset, count);
        Regex reg = new Regex("(]*>)", RegexOptions.IgnoreCase);
        Match m = reg.Match(s);

        if (m.Success)
        {
            string form = reg.Replace(m.Value, _url);
            int iform = m.Index;
            int lform = m.Length;
            s = s.Substring(0, iform) + form + s.Substring(iform + lform);
        }
        byte[] yaz = System.Text.UTF8Encoding.UTF8.GetBytes(s);
        _sink.Write(yaz, 0, yaz.Length);
    }

}
Then re-assign this.Response.Filter using new clsResponseModifier class with required replacement action url
 
protected void Page_Load(object sender, EventArgs e)
{
	this.Response.Filter = new clsResponseModifier(this.Response.Filter, "your_new_actional_url");
}



Leave a reply


Do you like this post?