Posts tagged deployment
Dynamic Robots.txt in ASP MVC
0I recently had the need to switch a robots.txt file dynamically, depending on whether a site was deployed to live or beta servers. The solution was very simple, ditch the real file and replace it with a controller action. This is a brief note on how I did this. I don’t pretend this is an ideal solution, it was something I knocked together before I went out for dinner.
The solution I went for was in three parts: a web.config transform, routing and the controller itself.
The web config transform for my live site looks like this:
<?xml version="1.0"?><configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform"> <appSettings> <add key="SiteStatus" value="live" xdt:Transform="SetAttributes" xdt:Locator="Match(key)"/> </appSettings></configuration>The route is equally simple:
routes.MapRoute( "Robots.txt", "Robots.txt", new { controller = "Robots", action = "RobotsText" });
And finally the actual controller:
public class RobotsController : Controller{ public FileContentResult RobotsText() { var content = "User-agent: *" + Environment.NewLine;
if (string.Equals(ConfigurationManager.AppSettings["SiteStatus"], "live", StringComparison.InvariantCultureIgnoreCase)) { content += "Disallow: /elmah.axd" + Environment.NewLine; content += "Sitemap: http://www.jacquelinewhite.co.uk/sitemap.xml"; } else { content += "Disallow: /" + Environment.NewLine; }
return File( Encoding.UTF8.GetBytes(content), "text/plain"); }}And there we have it, my robots.txt file now is customised on a per deployment basis.
An imperfect Hack to Keep App_Offline Showing When You Delete Your Web App
0In a simple world, a good solution for deploying web apps looks a bit like this:
- Remove server from load balancer
- Remove old site
- Install new site
- Verify installation locally
- Add server back into the load balancer
Nice and simple, now you just move onto the next server and repeat the process.
The problem I have is that I don’t have that kind of access to the load balancer; I have no mechanism for taking a given server out of action other than using the app_offline.htm file. This works tolerably, but it’s a dirty hack rather than an elegant solution. As part of my deployment process I always delete all files in the site, this is fine as all data is stored either in a database or in a different folder. This causes problems for asp.net as it stops seeing the site as an application and ignores the app_offline.htm file. That’s no good for users as they will start to see either the default 404 page, or a message about the directory listing not being found. A simple hack that I’ve put in is to add the app_offline.htm to the list of default documents; also I set the IIS 404 for that site to app_offline.htm. It’s safe to do this as my app sets its own 404 pages, so in normal operation the app_offline.htm is ignored. It’s not a perfect solution, but it is better than nothing and hopefully brings the number of users seeing duff pages down to a minimum.
What I really need now is a way for the app_offline.htm file to be ignored for requests coming to the local machine and then I can stop doing deployments in front of my users.