views:

35

answers:

3

Hi,

I'm trying to understand why this very simple HttpModule fails. The code is a precursor to a simple HttpUrlRewriter that I need to develop for a test project.

It appears that whenever I test the Request, and then execute a Response, the output is not written to the stream!

I've attached the debugger (VS 2008) to the module, and all 3 Response.Write statements in the below get executed, but only the two outer ones actually product output on the page. Have I missed a key understanding or caveat?

Thanks for any help.

Exeucting Environment: ASP.NET 3.5/WinXP/IIS 5

using System; using System.Collections.Generic; using System.Web; using System.Text; using System.Web.UI;

public class Interceptor : IHttpModule
{
    #region IHttpModule Members

    public void Dispose() { }

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

    private void TestHandler(object sender, EventArgs e)
    {
        HttpApplication app = (HttpApplication)sender;
        HttpContext ctx = app.Context;

        if (1 == 1)
        {
            ctx.Response.Write("Hello, 2"); // Works, as expected
        }

        string test = ctx.Request.Url.ToString();
        if (test.Contains("/images")) {
            ctx.Response.Write("Hello, never written"); // This code executes when the test passes, but nothing is ever written...
        }

        ctx.Response.Write("Hello"); // Works

    }

    #endregion
}
A: 

are you sure the breakpoint on that line is hit for the request you are monitoring? Be aware that if you have an image in that /images folder and a .aspx page that contains that image your module will be called twice in the development server (for the aspx and the image), and only once in a default IIS (only for the aspx file). When you have simultaneous requests to the module it's sometime hard to see which breakpoint is on which request.

Tudor Carean
A: 

If your requesting an image the Response stream will be different too!

Neil
Yeah, only after I told you what it was! Berk!
cashingin
A: 

The answer was, sadly, stupidity on my part. One of those classics that get overlooked!

The request, while containing /image, also happened to be for a binary file. Therefore the Response was written to the stream for that rather than the Page.

cashingin