C# – Custom C# HttpModule Infinite Redirect Loop

chttpmoduleinfinite-loop

I am writing a custom c# HttpModule that will handle requests from all file types. As a simple proof of concept I have setup the module by adding a reference to the httpModules section of the web config and added application extensions for the demo IIS website with a reference to the aspnet_isapi.dll so that it currently only intercepts request for ".htm" files

But even if there is no significant code in the "OnBeginRequest" event (code below) it causes an infinite redirect loop. I am using IIS 5 on XP Anyone got any ideas?

So far I have only seen HttpModule examples for use with ASPX files but surely you can configure the for any file type?

#region IHttpModule Members

        public void Dispose() { }

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

        /// 
        ///
        /// 
        /// 
        public void OnBeginRequest(Object s, EventArgs e)
        {
            HttpApplication context = s as HttpApplication;

            Uri currentURL = context.Request.Url;
            string pageName = currentURL.Segments.Last().ToLower();
        }
#endregion

Best Answer

OK. The problem was actually in the HttpModule itself.

It appears that you have to use the HttpApplication context in order for it to render on the client.

For Example after you have performed all your custom logic you need to write to the context:

context.Response.Write("/n/r");

//or

context.Response.Redirect("test.htm");

Everything then renders as you would expect