Sunday, February 22, 2009

HowTo Redirect WebResource.axd Requests To Another Server

Today an other trick in order to Redirect WebResource.axd Requests to Another Web Application Server. In collaboration with my friend Sebastien. V we made this HttpModule.

Here is the code
public class WebResourceRedirectorModule : IHttpModule
{
#region IHttpModule Members

public void Dispose()
{
}

public void Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(BeginRequest);
}

#endregion

private void BeginRequest(object sender, EventArgs e)
{
HttpRequest Request = HttpContext.Current.Request;
HttpResponse Response = HttpContext.Current.Response;
String Url = Request.Url.OriginalString;
String RemoteServer = "http://localhost:2847";

if (Url.Contains("WebResource.axd"))
{
Uri uri = new Uri(Url);
String RemoteUrl = RemoteServer + uri.PathAndQuery;
HttpWebRequest req = WebRequest.Create(RemoteUrl) as HttpWebRequest;
using (StreamReader sr = new StreamReader(req.GetResponse().GetResponseStream()))
{
String data = sr.ReadToEnd();
Response.Write(data);
Response.End();
}
}
}
}


Download Solutionhttp://www.blogger.com/img/blank.gif - WebResourceRedirector.zip

more

No comments:

Post a Comment