Adding Robots.txt to your ASP.NET MVC 3 Applications

One of the things I always forgot to add to my web applications is the Robots.txt file that Search Engines use to see what they should index.  This file and site maps help make your site easier to navigate by the bots and let them know what is legal and what you would rather not have the published in their engines.  I typically add any administrative pages or account pages even though they are protected by security, no need for the login page to be index if they sniff the link.

So how do you add Robots.txt to your MVC 3 application?  Glad you asked, here is a little code to get you started.

Sample Code

1.  Select the controller you would like to use for the robots.txt output.  I chose the HomeController in my application as I use it for most “top level” generic links like about us, contact us, index, etc. 

2. Create a method called Robots to handle the request.

#region -- Robots Method --

public ActionResult Robots

{

Response.ContentType = "text/plain";

return View;

}

#endregion

3. Add the Robots.cshtml view to your Controller’s View directory.  Here is the code I have in my view, yours will vary.

@{

Layout = null;

}

# robots.txt for @this.Request.Url.Host

User-agent: *

Disallow: /Administration/

Disallow: /Account/

4. Load up the class you are using to control your routes, if you are in an Area, this could your AreaRegistration class.  If you are at the top like I am and using the standard MVC template, this is probably the Global.asax.cs file.  Add your route to this file, mine looks like this.

routes.MapRoute("Robots.txt",

"robots.txt",

new { controller = "Home", action = "Robots" });

5. Compile and test.

Conclusion

If you have an internet facing site, the chances are you will have a bot find you are request this page.  You might as well give them the benefit of the doubt and let them know where you want them to go.  Also you will save yourself some error log when this page is requested and no controller is found.

Just like anything in ASP.NET, there are many ways to solve this riddle, if you use a different approach, please feel free to share it in the comments.

Technorati Tags:,,

This article is part of the GWB Archives. Original Author: Jeff Julian

New on Geeks with Blogs

  • We Won The One Award I Actually Care About

    Full Scale made the Inc. 5000 for the fifth year straight, the 12th listing across my three companies. Here is why the one award you cannot buy is worth stopping for.

  • Your Customers Build the Features Now

    I let a tool I liked sit dead for a year rather than build the features I wanted. An MCP server meant I never had to, and your customers can do the same to your product.

  • Get the Size of a Directory in Linux the Easy Way

    du -sh for the quick answer, ncdu for the cleanup, df for the disk itself: every command for checking directory size in Linux, plus why du and df never agree.

  • Vim Search and Replace: The Ultimate Guide

    One :%s command replaces every match in a file before a find dialog would even open. The Vim substitute patterns worth the muscle memory: flags, ranges, capture groups, and multi-file edits.