I experienced a strange behaviour with the DateTime class and formatting today. For some reason this error only occurs when you format a DateTime in certain cultures like Italian. The error is not present in English, Danish and most other cultures.

It happens when you use the DateTime.ToString and DateTime.ParseExact methods. When you write the following:

String date = DateTime.Now.ToString("yyyy-MM-dd HH:mm")

You would expect the date variable to be formatted like so 2008-01-05 11:30. In Italian it doesn’t. Instead it replaces the colon with a period which produces this 2008-01-05 11.30. This might be correctly formatted for the Italian culture, but I explicitly wrote I wanted the colon delimiter in the format string. However, there is a workaround.

All you need is to change the format string into a less obvious one. The following produces the right colon delimited date and time string:

String date = DateTime.Now.ToString("yyyy-MM-dd HH\\:mm")

Strangely enough, you have to escape the colon with two backslashes for it to work. This trick seems to work in all cultures.

There is a lot of new features on almost every objects in the BlogEngine.NET code base, so here are a few examples on how to take advantage of some of them. I’ll put up some new videos soon highlighting some of these features.

User control injection with parameters

The user control injection feature introduced in version 1.2 has been upgraded, so that it now let’s you insert property values. Let’s imagine you have a user control called poll.ascx with two public properties called Name (string) and MaxAnswers (int). You can now set those properties from the editor by separating the properties with a semicolon like this:

[ usercontrol: ~/poll.ascx Name=Name of poll; MaxAnswers=200]

JavaScript handler

The new JavaScript handler minifies and HTTP compresses .js files. You can serve your own .js files through the handler easily from your theme. The method that converts the original path to the .js file into the new js.axd handler is located at the BlogBasePage. So if you from the site.master need to add a custom JavaScript you can do it like so:

<script type="text/javascript" src="<%=(Page as BlogEngine.Core.Web.Controls.BlogBasePage).ResolveScriptUrl("/script.js")%>"></script>

Also, the JavaScript handler can function as a proxy between your blog and a remote JavaScript like the Google AdSense script. By feeding an absolute URL into the ResolvescriptUrl method, the handler now caches the script for three days and serves it minified and HTTP compressed. This proxy feature was created to mitigate the performance hit a site have by including remote scripts that are slow.

I use a remote script on this site that is ~1 second to respond to the request and it isn’t minified or compressed, so the load time of this page is 1 second faster with the new JavaScript handler.

TagCloud update

Some people have a lot of tags and a lot of these tags only contain 1 post. That can clutter the TagCloud, so now you have the ability to specify how many posts a tag should contain as a minimum to be shown in the TagCloud. The default is 1 post, but you can change it in your theme like so:

<blog:TagCloud MinimumPosts="2" runat="server" />

Extensions and CancelEventArgs

The System.ComponentModel.CancelEventArgs are now used for a various different events. It let’s you cancel an action like adding comments or receiving trackbacks. Here is what to do in order to take advantage of the new model:

// constructor
public MyExtension()
{
 TrackbackHandler.Received += new EventHandler<CancelEventArgs>(TrackbackHandler_Received);
}

// event handler
void TrackbackHandler_Received(object sender, CancelEventArgs e)
{
  if (HttpContext.Current.Request.UserHostAddress == "123.456.78.90")
     e.Cancel = true;
}

This is an example of how to cancel a trackback request from being processed based on the IP address of the requesting website.

More info will follow here, on the BlogEngine.NET website and at Al Nyveldt's blog.