31. March 2011 01:06
by Henrik Stenbæk
0 Comments
If you have one application on your IIS and several domain names pointing to it, it’s possible to have different robots.txt files served based on the current domain name.
1. Add a route
![CropperCapture[1] CropperCapture[1]](/image.axd?picture=Windows-Live-Writer/Dynamic-robots.txt-with-ASP.NET-MVC/4351DEE1/CropperCapture1_thumb.jpg)
2. Create the RobotsController
![CropperCapture[2] CropperCapture[2]](/image.axd?picture=Windows-Live-Writer/Dynamic-robots.txt-with-ASP.NET-MVC/064DAD4F/CropperCapture2_thumb.jpg)
3. The SeoHelper just implements a simple way of determine what robots.txt file to return
![CropperCapture[3] CropperCapture[3]](/image.axd?picture=Windows-Live-Writer/Dynamic-robots.txt-with-ASP.NET-MVC/52A540F0/CropperCapture3_thumb.jpg)
4. Add the different versions of the robot.txt to the site root – remark the content for the robots.txt could be served from any source, I have just chosen a simple model to keep it simple
![CropperCapture[4] CropperCapture[4]](/image.axd?picture=Windows-Live-Writer/Dynamic-robots.txt-with-ASP.NET-MVC/02EC15A7/CropperCapture4_thumb.jpg)
5. Run the site and request robots.txt
![CropperCapture[5] CropperCapture[5]](/image.axd?picture=Windows-Live-Writer/Dynamic-robots.txt-with-ASP.NET-MVC/3647D903/CropperCapture5_thumb.jpg)
![CropperCapture[6] CropperCapture[6]](/image.axd?picture=Windows-Live-Writer/Dynamic-robots.txt-with-ASP.NET-MVC/2011BDB1/CropperCapture6_thumb.jpg)
Get the source code: DynamicRobotsTxt.rar (21,66 kb)