I never thought that I was gonna write code for an ASP.NET Webforms project again. I’ve been creating all my projects in ASP.NET MVC since it hit beta. But, I guess Webforms is really good at some stuff, Coming up with quick and dirty web apps :)
Alright, So, I’ve got a web application which was created in ASP.NET MVC. The administrators of this web app want to control when the site is up. It’s basically a webapp for processing some stuff which happens only at day time. So, they don’t want the users (their agents) to go around and mess with the site during the night time or as soon as they get out of the office. We already had a nice web app with an administrator console through which the admins can do a lot of stuff like adding/de-activating users etc,. The administrators wanted a feature through which they could bring the site down or up using their admin page. A simple search showed up a lot of interesting articles, This one by Rick Strahl documents how you can take down an app, and bring it up without using “app_offline.htm” files. I didn’t want to take that route, because I think the app_offline way is the best, because it shuts down the application and unloads the application domain, You can find some more interesting info about it in this post by Scott Gu. Now, the downside of using this kind of approach is that, once the application is down, there is no way to bring it up using the same site.
Now, let’s see how we can use this nifty little feature which is available for asp.net 2.0+ apps to come up with an admin page through which we can take the site down or bring it back up.
1) The first thing you need to do is create a basic Webforms website or Web application from Visual Studio. On the default page you would want a few textboxes, radio buttons and a submit button, which looks like
2) This is the fragment of code which creates this GUI, Nothing too fancy.
3) Once we have that, it’s basically an exercise in creating some code which does the user/password matching and creates an app_offline.htm file in the right location.
The code is pretty much self explanatory, Hope it helps someone. You just have to be careful about a couple of things:
- You still have to run this on a different website
- You need to give the right privileges on the directory APP_OFFLINE_PATH to the user which runs the IIS worker process. For IIS7 this is the NETWORK SERVICE user by default.
And before I forget, You can get the source code for this project from my git repository hosted on github at http://github.com/minhajuddin/blog_demos/tree/master