Today I wrote a new dynamic RSS feed for a little project called Little Helper. It all works fine, but when I tried to validate it using FeedValidator, the pubDate was invalid. The validator told me that the date was not proper formatted using the RFC822 standard which looks something like this:

Wed, 27 Sep 2006 21:36:45 +0200.

Ok, that’s no problem right? Just format the DateTime correctly and you’re ready to go. Wrong!

This is what happens if you try to do just that:

DateTime.Now.ToString("r")

The output of that is:

Wed, 27 Sep 2006 21:49:19 GMT

GMT is an acceptable value for RFC822, but not for my location which should be +0200. It fails on all the machines I tried it on. If I then try to write the format string manually like this, it still doesn’t work.

DateTime.Now.ToString("ddd, dd MMM yyyy HH:mm:ss zzzz")

The output of that is:

Wed, 27 Sep 2006 21:36:45 +02:00

It is almost correct, but the time zone is not formatted correctly. After searching the web, it became clear that .NET does not let you format the DateTime to the RFC822 by its standard formatters. So, I had to write a little method that did just that.

/// <summary>

/// Converts a regular DateTime to a RFC822 date string.

/// </summary>

/// <returns>The specified date formatted as a RFC822 date string.</returns>

private static string GetRFC822Date(DateTime date)

{

  int offset = TimeZone.CurrentTimeZone.GetUtcOffset(DateTime.Now).Hours;

  string timeZone = "+" + offset.ToString().PadLeft(2, '0');

 

  if (offset < 0)

  {

    int i = offset * -1;

    timeZone = "-" + i.ToString().PadLeft(2, '0');

  }

 

  return date.ToString("ddd, dd MMM yyyy HH:mm:ss " + timeZone.PadRight(5, '0'));

}

Just give it a DateTime and it will return a properly formatted RFC822 string that will validate.

Response.Write(GetRFC822Date(DateTime.Now));

Note, that the RSS feed probably works fine without using a method like this, even though the FeedValidator doesn’t agree. At the end of the day, the feed validated.

In a recent web project I suddenly got an unexpected exception when I was uploading a file using a standard <asp:FileUpload /> control in ASP.NET 2.0. I’ve only been testing the website in Firefox at that point and everything worked great. The error only occurred in IE 6 and 7.

Here is a little dummy code that I’ll use to demonstrate the difference.

<asp:FileUpload runat="server" ID="txtUpload" />

 

private void Upload()

{

  string fileName = txtUpload.PostedFile.FileName;

  Response.Write(fileName);

}

If I try to upload the file C:\test.txt then the fileName variable will be set to test.txt in Firefox, but C:\test.txt in IE. The reason for the exception was that I used that filename to name the file on the server when it was uploaded and saved to disk. In Firefox that was no problem because I just added the filename to a folder path on the server like “C:\\uploads\\” + fileName. But in IE that would result in this file path C:\uploads\C:\test.txt which clearly is wrong.

I found the solution within the ASP.NET object model. It is very simple, but gave me a lot of gray hairs before figuring it out.

>

// Wrong! work only in Firefox

string fileName = txtUpload.PostedFile.FileName;

 

// Correct! Works on all browsers

string fileName = txtUpload.FileName;

Strange little quirk, but as long as you know your way around it, it’s not a big deal.

>