Dynamic Robots.txt in ASP MVC

I 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:

1 2 3 4 5 6
<?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:

1 2 3 4 5 6 7 8 9
routes.MapRoute(
"Robots.txt",
"Robots.txt",
new
{
controller = "Robots",
action = "RobotsText"
}
);
view raw routing.cs This Gist brought to you by GitHub.

And finally the actual controller:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
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.

Posted in .net, asp net mvc and tagged as ,
2 comments Submit a comment
  • Marie Sligh commented on 2012/03/02 at 16:21

    Reply

    Thank you! Just what I was looking for!

  • Sweet! .. what about an iis redirect rule ? this might work too ..maybe more easily ?

    Ie conditionally serve a robots.txt file depending on the domain of the request.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>