I’ve always been a big fan of FxCop even though I’ve had my share of problems with some of the rules. The new beta version fixes all that. It works much better with ASP.NET which the previous versions didn’t do very well at all.

There are also some new rules to help you write better software. Especially two rules are popping up everywhere and those are the Specify StringComparison and Normalize strings to uppercase rules.

The Specify StringComparison rule helps you to do string searching in a better way by always specifying a StringComparison enum when you do string manipulation such as IndexOf and LastIndexOf etc.

The Normalize strings to uppercase rule makes sure you use ToUpper or ToUpperInvariant when you normalize strings. It is more efficient to upper case a string than to lower case it.

The design looks very much the same as the 1.35 version with some minor tweaks here and there. It also supports .NET 3.5. Overall, it’s a really good application and a much needed upgrade. You can download it here.

Today I was working on a web page that performs an AJAX call on unload, so when a person navigates away from the page, the AJAX call is triggered. The goal was to register that the visitor navigated away from the page so just a one-way call was needed.

I started out using a simple ASP.NET client callback and it worked – or so I thought. When the next page on the same website is loaded, a JavaScript error occurred because the __pendingcallback variable was null.

Then I made the callback asynchronous instead of synchronous but it didn’t help. Then a colleague suggested that I should use an ASP.NET AJAX script service call instead. That didn’t help. A JavaScript error still occurred though it was a different one but caused by the same issue.

So I started doing what I should have done from the beginning – do it old school. It’s faster, more reliable, uses GET, light weight and completely cross-browser supported. I created an HttpHandler that took a few URL parameters. I could have used the same page but for this scenario a handler was more appropriate and they are always faster.

Then I added this piece of code in the unload event handler in JavaScript:

[code:html]

<script type="text/javascript">
function UnLoadHandler()
{
  var img = new Image();
  img.src = "/handler.ashx?id=1234";
}

window.onunload = UnLoadHandler;
</script>

[/code]

The UnLoadHandler() method performs a GET request to the handler by preloading it into an Image object. This is of course not AJAX according to the definition, but it is asynchronous and uses JavaScript. I guess that makes it AJ.