A while back, I wanted to include the then new URL Routing features in C# 3.0 in a website I was building. I didn’t want to use MVC though. I found a great article by Scott Mitchell at 4 Guys From Rolla that had almost everything I wanted to know about Using ASP.NET Routing Without ASP.NET MVC.
There was one thing though that was missing. How to deal with special files like the favicon.ico file. I found out that the serving was not sending the favicon.ico file but was serving my not found page that my registered routes defaults too when it could find no activity for /{Activity}.
I wrote a question on Stack Overflow, entitled, Using URL Routing for Web Forms and StopRoutingHandler for Favicon, I didn’t get any answers and I eventually answered it myself. Since then over 1,000 people have viewed my question so there must be some merit to it.
The easy solution is to just use this code:
routes.Add("RSS", new Route("syndication.rss", new RSSRouteHandler()));
One thing I didn’t realize and it drove me nuts was that Firefox would continue to work incorrectly even when I cleared the cache and refreshed with ctrl+F5 and stopped and restarted VS2010 debugging. The only way to get my favicon to work was to close Firefox and open it again and then run the debugger.
Once I figured this out I found a number of other helpful files to use the StopRoutingHandler on.
routes.Add(new Route("robots.txt", new StopRoutingHandler()));
routes.Add(new Route("{resource}.axd/{*pathInfo}", new StopRoutingHandler()));
routes.Add("RSS", new Route("syndication.rss", new RSSRouteHandler()));
Since you always need a robots.txt you will always need line 1. If you are using any of Microsoft’s Ajax generating code then you will need line 2 because Microsoft creates their Ajax files as .axd’s. Finally, you will probably have a different RSS feed than me so your Route string will need to be changed for line 3 but if you have an RSS feed you will need it.
These 4 lines of code made a huge difference for me and it did take me a little while to figure them out. Hopefully, this code will help others.
Recent Comments