In particular, I would like to deploy a different robots.txt file for different build configurations. We have a staging environment that is publicly available on the web, but we disallow everything in the robots.txt file for that environment so it doesn't get indexed and compete with our production site.
Currently, we just manually copy the previous version of the robots.txt file in production into the new folder each time we deploy. Is there a way to put both versions into the project and deploy a particular one based on the build configuration? Or is there a more 'correct' way to handle this?
I use a pre-build event to do this. There is a really good article on it here: http://www.aaroncoleman.net/post/2011/10/28/Simple-ASPNET-robotstxt-transform-like-XDT.aspx
In summary use the following script in the pre-build event filed in your project's properties.
copy $(ProjectDir)Content\robots_txt\robots.$(ConfigurationName).txt $(ProjectDir)robots.txt /Y
Then you can store different file versions named according to the build config, and the script will copy the contents to the new file.
I had the same problem and I found this article on www.asp.net: http://www.asp.net/mvc/overview/deployment/visual-studio-web-deployment/deploying-extra-files
As your situation is similar to mine, I had to adjust the technique in the article by creating separate subfolders under the ExtraFiles folder for each build profile. And then update each of your .pubxml files to refer to each build profile in that _CustomFiles node:
<_CustomFiles Include="..\ExtraFiles\BuildProfile1\**\*" />
For those of us, who are used to XML web.config transformations, the solution for simple files, like robots.txt, might be as easy as to put all the relevant info into a web.config app setting and on application startup, just generate the exact file(s) you need.
For example, in our web.config, we might have lines like these:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="robots.txt" value="
User-agent: *
Disallow: /
" />
</appSettings>
</configuration>
In our transformation XML files (web.stage.config, web.prod.config), we would, of course transform the value of that setting. And at last, in our code, we could just generate the file with transformed content:
static void Main(string[] args)
{
var fileName = "robots.txt";
var fileContent = ConfigurationManager.AppSettings[fileName];
File.WriteAllText(fileName, fileContent);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With