Jeff Julian's Blog

Just Another Geek With A Blog


News

+Jeff Julian GWB Tag







SharePoint Consulting (Kansas City Metro)

SharePoint Training

AJI Software - Careers





Check out some of my wife's products!:

Cake Mixes - Made With Scratch
Cookie Mixes - Made With Scratch
Marinades - Made With Scratch

My Stats

  • Posts - 707
  • Comments - 634
  • Trackbacks - 925

Twitter












Tag Cloud


Recent Comments


Recent Posts


Archives


Post Categories


Image Galleries


My Company


My Fav. Sites



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

Comments

Gravatar # re: Adding Robots.txt to your ASP.NET MVC 3 Applications
Posted by Richard Moss on 4/25/2012 8:00 AM
Why would you do this over just simply placing a "normal" robots.txt in the site root? Adding a route and view does seem somewhat overkill when the plain text file works fine!

Regards;
Richard Moss
Gravatar # re: Adding Robots.txt to your ASP.NET MVC 3 Applications
Posted by Jeff Julian on 4/25/2012 8:17 AM
I totally agree that if you are not going to need dynamic syntax for site map links or protected directories, keep the static text file. In my case, we often have large amounts of content leading to a variable amount of sitemaps, so being able to dynamically generate the robots.txt helps.
Gravatar # re: Adding Robots.txt to your ASP.NET MVC 3 Applications
Posted by Leniel Macaferi on 8/6/2012 11:04 AM
Hey Jeff,

Nice one!

I had to enable RAMFAR so that the route could be caught instead of being handled by the staticfilehandler. It was giving me a 404 since there's no static file in the game (it's dynamically created):

<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
...

Look here for the details: http://stackoverflow.com/q/11257768/114029


Thanks,

Leniel
Gravatar # re: Adding Robots.txt to your ASP.NET MVC 3 Applications
Posted by Anish on 8/10/2012 9:02 PM
Thanks, this was very useful. Do you have any tutorial for creating sitemap for MVC 3 web application. Content i am looking at is very dynamic.
Cheers
Gravatar # re: Adding Robots.txt to your ASP.NET MVC 3 Applications
Posted by Ken on 10/24/2012 11:43 AM
I thought it was useful. A dynamic robots.txt was very helpful, since we have to deploy to several servers and need to map the sitemap to the full URL. Using robots.txt means you don't have to edit for everyone server, local, dev, test, staging, production.
Gravatar # re: Adding Robots.txt to your ASP.NET MVC 3 Applications
Posted by ppl on 2/21/2014 5:48 PM
Thanks, it very usefull!
Post A Comment
Title:
Name:
Email:
Comment:
Verification: