Alex Rivera | Logout

IIS7, RewritePath and IIS log files

Asked 2008-12-09T17:15:27.070
10

I am using Context.RewritePath() in ASP.NET 3.5 application running on IIS7.

I am doing it in application BeginRequest event and everything works file.

Requests for /sports are correctly rewritten to default.aspx?id=1, and so on.

The problem is that in my IIS log I see GET requests for /Default.aspx?id=1 and not for /sports.

This kind of code worked perfectly under IIS6.

Using Microsoft Rewrite module is not an option, due to some business logic which has to be implemented.

Thanks.

EDIT:

It seems my handler is too early in the pipeline, but if I move the logic to a later event, than the whole rewrite thing doesn't work (it's too late, StaticFileHandler picks up my request).

I googled and googled, asked around, can't believe that nobody has this problem?

EDIT:

Yikes! Here's what I found on the IIS forum:

"This is because in integrated mode, IIS and asp.net share a common pipeline and the RewritePath is now seen by IIS, while in IIS6, it was not even seen by IIS - you can workaround this by using classic mode which would behave like IIS6."

Final update: Please take a look at my answer below, I've updated it with results after more than a year in production environment.

Edit
Report

2 Answers

4

You could set the path back to the original value after the request has been processed but before the IIS logging module writes the log entry.

For example, this module rewrites the path on BeginRequest and then sets it back to the original value on EndRequest. When this module is used the original path appears in the IIS log file:

public class RewriteModule : IHttpModule
{
    public void Init(HttpApplication context)
    {
        context.BeginRequest += OnBeginRequest;
        context.EndRequest += OnEndRequest;
    }

    static void OnBeginRequest(object sender, EventArgs e)
    {
        var app = (HttpApplication)sender;
        app.Context.Items["OriginalPath"] = app.Context.Request.Path;
        app.Context.RewritePath("Default.aspx?id=1");
    }

    static void OnEndRequest(object sender, EventArgs e)
    {
        var app = (HttpApplication)sender;
        var originalPath = app.Context.Items["OriginalPath"] as string;
        if (originalPath != null)
        {
            app.Context.RewritePath(originalPath);
        }
    }

    public void Dispose()
    {

    }
}
answered 2009-02-17T18:14:37.280
2

I've had exactly the same problem. One way around this is to use Server.Transfer instead of Context.RewritePath. Server.Transfer doesn't restart the entire page lifecycle so the original URL will still be logged. Be sure to pass "true" for the "preserveForm" parameter so that the QueryString and Form collections are available to the 2nd page.

answered 2009-02-17T18:34:12.693

Your Answer