Hi,
I had the same problem (AJAX + url rewriting) and I implemented a simple response filter:
using System;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
using System.Web;
public class AjaxPageFilter : Stream
{
Stream responseStream;
long position;
StringBuilder responseHtml;
public AjaxPageFilter(Stream inputStream)
{
responseStream = inputStream;
responseHtml = 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()
{
responseStream.Close ();
}
public override void Flush()
{
responseStream.Flush ();
}
public override long Length
{
get { return 0; }
}
public override long Position
{
get { return position; }
set { position = value; }
}
public override long Seek(long offset, SeekOrigin origin)
{
return responseStream.Seek (offset, origin);
}
public override void SetLength(long length)
{
responseStream.SetLength (length);
}
public override int Read(byte[] buffer, int offset, int count)
{
return responseStream.Read (buffer, offset, count);
}
public override void Write(byte[] buffer, int offset, int count)
{
string finalHtml = System.Text.UTF8Encoding.UTF8.GetString(buffer, offset, count);
// Transform the response
string formAction = Util.OriginalRequestUrl;
if (!String.IsNullOrEmpty(formAction))
{
finalHtml = Regex.Replace(finalHtml, "\\|\\d+\\|formAction\\|\\|[^\\|]+\\|",
"|" + formAction.Length.ToString() + "|formAction||" + formAction + "|",
RegexOptions.IgnoreCase);
}
// Write the response
byte[] data = System.Text.UTF8Encoding.UTF8.GetBytes(finalHtml);
responseStream.Write(data, 0, data.Length);
}
}
You should replace "Util.OriginalRequestUrl" with your code.
This filter can be attached in some HttpModule for example like this:
public class UrlRewriter : IHttpModule
{
public UrlRewriter()
{
}
public void Dispose()
{
}
public void Init(HttpApplication application)
{
application.BeginRequest += new EventHandler(application_BeginRequest);
application.ReleaseRequestState += new EventHandler(InstallResponseFilter);
}
void application_BeginRequest(object sender, EventArgs e)
{
HttpContext context = HttpContext.Current;
// original URL
string url = context.Server.UrlDecode(context.Request.Url.ToString());
// store original URL
Util.OriginalRequestUrl = url;
// ...
}
private void InstallResponseFilter(object sender, EventArgs e)
{
HttpResponse response = HttpContext.Current.Response;
if (response.ContentType == "text/plain")
response.Filter = new AjaxPageFilter(response.Filter);
}
} Hope that helps.
Feodor Fitsner, MCSD
CEO, DotNetPanel Company
DotNetPanel - Secure and Scalable Hosting Management Solution
http://www.dotnetpanel.com