Tuesday, July 11, 2006

Google Site Maps and Http Handlers Part 2

In my last post I discussed using the method for the section of the web.config file. The remove element comes in handy if you’ve added an HttpHandler to the root of your Web site, but don’t want that handler to be loaded in sites below the root Web site.

Today I found a better way to control inheritance of the Http Handlers. The <add> element for Http Handlers has an attribute, validate, which tells ASP.NET whether it should load the class immediately or wait until a request comes. By default the value is set to true. Setting validate to false prevents the class from being loaded and thus eliminates the error.

In the case of the Google Site Maps handler, we had the following where validate was defaulting to true

<httpHandlers> <add verb="*" path="googlesitemap.axd" type="Newtonsoft.GoogleSitemap.GoogleSitemapHandler, Newtonsoft.GoogleSitemap"/> </httpHandlers>

We can change the add element to this to prevent ASP.NET from loading the googlesitemap.dll until we browse to googlesitemap.axd.

<httpHandlers> <add verb="*" path="googlesitemap.axd" type="Newtonsoft.GoogleSitemap.GoogleSitemapHandler, Newtonsoft.GoogleSitemap" validate="false”/> </httpHandlers>

Of course within your subsites, ASP.NET will generate an error if you try to browse to http://yourdomain.com/subsite/googlesitemap.axd. Ideally I wish there were an attribute like inheritance=false but until then this gets the job done.

Friday, July 07, 2006

Google Site Maps and Http Handlers

As you may have read about by now, Google allows you to publish an XML site map that helps their Web crawlers find all the pages in your site. Newtonsoft was nice enough to publish an assembly that reads your ASP.NET sitemap and produces a Google sitemap for you. You only need to add one line of code to your web.config, which sets up an HttpHandler. Then you can point Google to the path specified in the handler, and you’re done. This allows you to maintain just the ASP.NET sitemap yet still take advantage of Google’s new service.

Setup of the Newtonsoft GoogleSitemap is easy, and is well documented on their site.

One gotcha to watch out for, though, occurs if you have Web applications in folders below the application you add this HttpHandler to. For instance, if you add the HttpHandler to your root web.config, as follows, it will be inherited by all the applications beyond the root.

<httpHandlers> <add verb="*" path="googlesitemap.axd" type="Newtonsoft.GoogleSitemap.GoogleSitemapHandler, Newtonsoft.GoogleSitemap"/> </httpHandlers>

So here you’re saying you can go to http://www.yourdomain.com/googlesitemap.axd to view your Google-ready Sitemap. But what if you have another application at http://www.yourdomain.com/subfolder/myapp.aspx? In the subsite the HttpHandler will be inherited, which means that http://www.yourdomain.com/subfolder/googlesitemap.axd should work too.

The problem is that the DLL you download from Newtonsoft is located in the root’s bin folder. When you visit http://www.yourdomain.com/googlesitemap.axd the DLL is found in the root’s bin folder. However, any applications below the root go looking for that assembly within their bin folder. When it can’t be found you’re stuck with the Could not load file or assembly 'Newtonsoft.GoogleSitemap' or one of its dependencies error.

The simplest way to fix this issue is to remove the httpHandler in the subsite. That can be accomplished by adding the following remove element in your subsite’s web.config, as shown below:

<httpHandlers> <remove verb="*" path="googlesitemap.axd" /> </httpHandlers>