26 March 2010
ASP.NET MVC Best Practices – Routes to Ignore
Routing is a key feature from the ASP.NET MVC. If you are not familiar with this concept, take a look here, because I’m not going to explain it. I’m going to share some routes that you should ignore in your configuration.
So maybe you are wondering, why should I ignore some routes? And the simple answer is: Because most of the time, different search engines/crawlers/bots will try to index your site and they will request for specific files. For example, Google will try to access the following file: http://yoursite.com/robots.txt. The robots.txt file is used to to give instructions about your site to web robots. More info can be found here. Up to now, everything sounds great, but if your site does not have robots.txt file, the ASP.NET MVC framework will raise an exception upon a request like the above. So your log, where the unhandled exceptions are reported can be flooded with messages like:
System.Web.HttpException : A public action method 'robots.txt' could not be found
on controller 'YourPage.Controllers.YourController'.
Obviously, there are two ways to handle this – provide a dummy robots.txt file or instruct the ASP.NET MVC framework to ignore such URL’s. I prefer the second approach. In this post I will try to summarize all the URL’s that should be ignored by the ASP.NET application. So, here they are:
Here is the code, taken from the Global.asax.cs file that should ignore some of the URL’s outlined above:
I will update the list above as soon as I find a new URL that should be ignored, so stay tuned. As well you are welcome to contribute to this list by posting a comment.
So maybe you are wondering, why should I ignore some routes? And the simple answer is: Because most of the time, different search engines/crawlers/bots will try to index your site and they will request for specific files. For example, Google will try to access the following file: http://yoursite.com/robots.txt. The robots.txt file is used to to give instructions about your site to web robots. More info can be found here. Up to now, everything sounds great, but if your site does not have robots.txt file, the ASP.NET MVC framework will raise an exception upon a request like the above. So your log, where the unhandled exceptions are reported can be flooded with messages like:
System.Web.HttpException : A public action method 'robots.txt' could not be found
on controller 'YourPage.Controllers.YourController'.
Obviously, there are two ways to handle this – provide a dummy robots.txt file or instruct the ASP.NET MVC framework to ignore such URL’s. I prefer the second approach. In this post I will try to summarize all the URL’s that should be ignored by the ASP.NET application. So, here they are:
- ~/robots.txt
- ~/sitemap
- ~/sitemap.gz
- ~/sitemap.xml
- ~/sitemap.xml.gz
- ~/google_sitemap.xml
- ~/google_sitemap.xml.gz
- ~/favicon.ico
- ~/mobile
- ~/iphone
- ~/apple-touch-icon.png
- ~/sitemap.txt
- ~/sitemap.rss
Here is the code, taken from the Global.asax.cs file that should ignore some of the URL’s outlined above:
public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.IgnoreRoute("robots.txt"); routes.IgnoreRoute("sitemap"); routes.IgnoreRoute("sitemap.gz"); routes.IgnoreRoute("sitemap.xml"); routes.IgnoreRoute("sitemap.xml.gz"); routes.IgnoreRoute("google_sitemap.xml"); routes.IgnoreRoute("google_sitemap.xml.gz"); routes.IgnoreRoute("favicon.ico"); //Rest of the code is ommited }
Comments:
Links to this post:
<< Home
Hi there,
Just read your post and wanted to say thank you for the link to my sitemap article.
You might also want to add sitemap.txt and sitemap.rss as I've seen both of these generated by some sitemap tools :)
Regards
John
Just read your post and wanted to say thank you for the link to my sitemap article.
You might also want to add sitemap.txt and sitemap.rss as I've seen both of these generated by some sitemap tools :)
Regards
John
Hi John,
Thanks for the comment. I have added to the list sitemap.txt and sitemap.rss :)
Regards
Stephan
Post a Comment
Thanks for the comment. I have added to the list sitemap.txt and sitemap.rss :)
Regards
Stephan
Links to this post:
<< Home
|
|

