ASP.NET HttpModule that detects debug mode on production servers
July 04, 2008 by Daniel HoelblingSo, after reading some stuff about HttpModules lately I thought it would be fun to create one myself. And, so I came up with something that might be useful in some scenarios.
A HttpModule that will cry foul when you try to put up a ASP.NET website with debug="true" onto a production server.
It is actually rather simple. Just a standard HttpModule that will check wether the request is local and wether debug is enabled. The local request check is done to make the module transparent to you while developing the app on cassini, while you'll see it instantly when you copy it over to your web server.
The code is rather simple:
public class DebugWarner : IHttpModule
{
public void Dispose()
{ }
public void Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(context_BeginRequest);
}
void context_BeginRequest(object sender, EventArgs e)
{
if (!HttpContext.Current.Request.IsLocal &&
HttpContext.Current.IsDebuggingEnabled)
{
HttpContext.Current.Response.Write(
"<div style=\"background: #ea999d; border: 1px solid black; width: 500px; text-align: center;\">\n" +
"<h1>Debug Mode is enabled!</h1><p>This severely impacts Webserver performance<br />" +
"Set <compilation debug="false"> in your web.config</p>" +
"</div>");
}
}
}
Making this work is quite easy too, simply put the above class in your App_Code directory and add this line to your
<add name="DebugWarner" type="DebugWarner"/>
If you now try to access the page with debug="true" you'll get: