Monday, February 28, 2011

DateTime formatting in C#

I use a custom format to show DateTime to user: "dd/MM/yyyy".

var nowString = now.ToString("dd/MM/yyyy");

The reason, I use the custom formatting, is to be not depended on user regional settings.

So, no matter what user's locale is, I expect the same format for a date.

But today I found that it does not work as I expect. On one of the target machines I got "28.02.2011" instead of expected "28/02/2011".

Why slash (/) is replaced by dot (.)? Is format string not enough explicit?

I got the answer on MSDN (RTFM!): The slash (/) is not a literal, but a pattern for the default date separator defined in DateTimeFormatInfo.DateSeparator. It has to be escaped (leading with back slash (\)), for being reproduced literally.

I fix my format string to be @"dd\/MM\/yyyy"

var nowString = now.ToString(@"dd\/MM\/yyyy");

Now it works!

Sunday, February 27, 2011

Profiling NHibernate 3 application with NHProf

NHProf is a great too for profiling a NHibernate application. It is powerful and easy to use.

I used it for a long time, but recently I ran into troubles when I tried to capture DB queries in my newly started web application using NHibernate 3.0.

As many times before I downloaded latest NHibernate release, created a new web application, added a references to NHibernate.dll and all dependencies, added a reference to HibernatingRhinos.Profiler.Appender.dll and put a code, initializing NHProf - HibernatingRhinos.Profiler.Appender.NHibernate.NHibernateProfiler.Initialize(); in Application_Start.

Thursday, February 17, 2011

Microsoft's free tool for PHP development

It sounds ironical, but Microsoft really provides a great tool for web development supporting PHP.

Welcome WebMatrix!

At first look it is yet another tool to support the native Microsoft .NET technology. In fact I don't remember that Microsoft ever supported non their stuff.

But the world is changing and now WebMatrix is not only tool for .NET developing. With WebMatrix you can write a pure PHP web application! It is amazing! Isn't it?

In this post will show you how to start a new PHP web application with WebMatrix.

Wednesday, February 16, 2011

ASP.NET MVC 3 Extensionless URLs on IIS 7 classic mode

Prior ASP.NET 4.0, the only way to handle extensionless URLs was setting runAllManagedModulesForAllRequests=”true” (IIS 7 integrated mode) or mapping a wildcard (IIS 6 and IIS 7 in classic mode). That impacts the performance of static requests (HTML, JPG, GIF, CSS, JS, etc), because a all requests are handled by ASP.NET and runs application pipeline with all attached modules.

ASP.NET v4.0 provides a better way to enable extensionless URLs routing.
There is a new feature that allows extensionless URLs to be directed into managed code, without a wildcard mapping or setting runAllManagedModulesForAllRequests=”true”.
Because of this feature all static resources requests are not longer handled by ASP.NET, and consequentially, they are no longer suffered by performance impact.

It works by default on IIS 6. You may read more about this feature on IIS 6 here .

But IIS 7 surprises a lot! It does not support this feature by default, and I spend an hours to find solution.

The solution is called IIS 7 QFE and steps to download it can be found at http://support.microsoft.com/kb/980368.

I don't know why they did not supply this with .NET 4 installation, they know it better.