Jan
20
2011

Using IHttpModule to overlay a website

I ran into the need to shut down an application for maintenance. What I needed is a plugin that I could drop on any website and it will create an overlay on top and disable all functionality. It had to also display a message notifying the users what's going on. Using an HttpModule seems to be the most logical way to go about that. First I create a class that inherits IHttpModule. In the implementation of the BeginRequest I inject a div overlay and the message into the body of the page. void context_BeginRequest(object sender, EventArgs e) { System.Web.HttpContext context = System.Web.HttpContext.Current; string message = System.Configuration.ConfigurationManager.AppSettings["WarningMessage"]; if (String.IsNullOrWhiteSpace(message)) message = "The web site is down for maintenance. Please check back again later."; context.Response.Filter = new OverlayInjector(context.Response.Filter, message); } I also created OverlayInjector : System.IO.Stream class to help with the injection overriding the Write method like that: public override void Write(byte[] buffer, int offset, int count) { var data = new byte[count]; Buffer.BlockCopy(buffer, offset, data, 0, count); string html = Encoding.Default.GetString(buffer); html = html.Replace("", "<div style='position: absolute; left: 100px; top: 250px; z-index: 999; color: red; font-weight: bold; font-size: 1.5em; width: 75%; background-color: #ffffcc; padding: 10px; text-align: center;'>" + _message + "</div> byte[] outdata = Encoding.Default.GetBytes(html); _stream.Write(outdata, 0, outdata.GetLength(0)); } Finally all I need to do is drop the dll into the bin of the website I want to implement this to and add two entries into the web.config file: <appSettings> <add key="WarningMessage" value="Down for maintanence check back on 1/1/2020"/> </appSettings> <httpModules> <add name="HttpModuleOverlay.Overlay" type="HttpModuleOverlay.Overlay"/> </httpModules> You can download the code here. HttpModuleOverlay.rar (16.64 kb)

Month List