Final resort for modifying your HTML output - Part 1

Using Response Filter

For the first method of modifying the html output I can suggest using a Response Filter. This i a filter that you can attach to the response stream which means that all output is sent through your specific filter before it is outputted to the client.

This means that we can make any modifications to the output that we should desire.

As described in my intro the first solution to my problem can be solved using the following lines of code


First I add all this code to a file residing in the app_code folder of my website

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

public class RegexModule : IHttpModule
    public void Dispose()
    public void Init(HttpApplication context)
    { context.ReleaseRequestState += new EventHandler(InstallResponseFilter); }
    private void InstallResponseFilter(object sender, EventArgs e)
        HttpResponse response = HttpContext.Current.Response;
        HttpRequest request = HttpContext.Current.Request;
        response.Filter = new Replacer(response.Filter, HttpContext.Current.Response.ContentEncoding);

public class Replacer : Stream
    private static Regex ACTIONTARGETREPLACE = new Regex(@"<form.+action=""([^""]+)""[^>]+>", RegexOptions.Compiled);
    private static Regex eof = new Regex("</html>", RegexOptions.IgnoreCase | RegexOptions.Compiled);
    /// <summary>
    /// This methods takes all input which is normally sent directly to the browser and saves it all to an internal buffer
    /// which can then be manipulated
    /// </summary>
    /// <param name="buffer"></param>
    /// <param name="offset"></param>
    /// <param name="count"></param>
    public override void Write(byte[] buffer, int offset, int count)
        string strBuffer = encoding.GetString(buffer, offset, count);

        // ---------------------------------
        // Wait for the closing </html> tag since the write method is called several times before it should actually be sent to the browser
        // ---------------------------------
        if (!eof.IsMatch(strBuffer))
            string finalHtml = resulthtml.ToString();
            string outputHtml = ACTIONTARGETREPLACE.Replace(finalHtml, delegate(Match m)
                return m.Value.Replace(m.Groups[1].Value, "NEWACTIONTARGET");
            byte[] data = encoding.GetBytes(outputHtml);
            response.Write(data, 0, data.Length);
    Stream response;
    StringBuilder resulthtml;
    Encoding encoding;
    public Replacer(Stream inputStream, Encoding encoding)
        response = inputStream;
        this.encoding = encoding;
        resulthtml = new StringBuilder();
    public override bool CanRead
    { get { return true; } }
    public override bool CanSeek
    { get { return true; } }
    public override bool CanWrite
    { get { return true; } }
    public override void Close()
    { response.Close(); }
    public override void Flush()
    { response.Flush(); }
    public override long Length
    { get { return response.Length; } }
    public override long Position
    { get; set; }
    public override long Seek(long offset, SeekOrigin origin)
    { return response.Seek(offset, origin); }
    public override void SetLength(long length)
    { response.SetLength(length); }
    public override int Read(byte[] buffer, int offset, int count)
    { return response.Read(buffer, offset, count); }

Then I add the following line to my web.config file in the http modules section

<add name="Replacer" type="RegexModule, App_Code"/>

After I have done these changes all the form tags on my website now have their action property set to "NEWACTIONTARGET" as specified by my code

Comments are closed