Click here to Skip to main content
Licence CPOL
First Posted 19 Jan 2008
Views 19,403
Bookmarked 12 times

Change your ASP.NET Form's Action attribute with Response.Filter

By | 19 Jan 2008 | Article
A simple way to change an ASP.NET Form's Action attribute with Response.Filter.

Introduction

Sometimes you need to change an ASP.NET page's form's Action attribute. Sure that, there are many ways.. And this is one (simple) of them.

Using the code

Just create a class in the app_code folder, and copy and paste the following lines:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text.RegularExpressions;

public class cdsnetFormActionModifier : Stream
{
    private Stream _sink;
    private long _position;
    string _url;
    public cdsnetFormActionModifier(Stream sink, string url)
    {
        _sink = sink;
        _url = "$1" + url + "$3";
    }

    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 long Seek(long offset, System.IO.SeekOrigin direction)
    {
        return _sink.Seek(offset, direction);
    }

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

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

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

    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("(<form.*action=\")([^\"]*)" + 
                              "(\"[^>]*>)", 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);
    }
}

And configure the Load event of any page you want, like below:

protected void Page_Load(object sender, EventArgs e)
{
    this.Response.Filter = 
      new cdsnetFormActionModifier(this.Response.Filter, "your_url");
}

Please change the string "your_url" with the client location of your destination page. That's all! Enjoy!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Tolgahan ALBAYRAK

Software Developer (Senior)

Turkey Turkey

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralDo you know of a VB version? PinmemberMember 78493561:52 19 Apr '11  
GeneralLooking for something to override the FORM ACTION Pinmembervbeedotnet14:51 6 Jan '09  
GeneralNice Idea Pinmemberdinomouse7:21 19 Jan '08  
GeneralRe: Nice Idea Pinmemberlrwilson2:48 28 Aug '09  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 19 Jan 2008
Article Copyright 2008 by Tolgahan ALBAYRAK
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid