I work a lot with httphandlers (.ashx) in ASP.NET – mostly to generate dynamic XML files for Macromedia/Adobe Flash consumption. It works great and the performance is great in httphandlers compared to webforms, but there is one major problem that Microsoft somehow ignored in Visual Studio 2005: Httphandlers does not support collapsing regions.

By regions I mean these

#region Name of region

#endregion

You can do regions in httphandlers like everywhere else in Visual Studio, but they will not collapse, so they becomes somewhat pointless. I’ve found this to be a huge problem for the maintainability of httphandlers, because you cannot group the different methods in logical containers. So, you’ll sometimes end up with a very large class and the only means of logical separation of code is to write comments.

This takes me back to the days of ASP and PHP where you separated the logic by comments because you would write the code in blocks for interpretation by the ASP/PHP web server engine. I can’t imagine I’m the only one finding this a problem, and I’m sure that Microsoft is aware of the issues it causes.

Maybe there is a registry setting that tells which file types Visual Studio is allowed to do collapsible regions in. I haven’t found the answer on the web, but If I do, I’ll post it here.

Today, I needed a function that would look through some text input and check for any URLs and the URLs should then be turned into hyperlinks. In other words, I had to turn this www.someurl.com into <a href="http://www.someurl.com" target="_blank">www.someurl.com</a>. It should also be able to handle URLs that starts with http:\\ and not just www.

I needed this functionality in a forum/comment application and it had to do the conversion at runtime. I wrote a simple regular expression for the URLs starting with http:\\ and another for URL's starting with www. These to regular expression are then combined into one method that takes a body of text and replaces all URLs with a proper hyperlink.

Here's what I came up with in C#:

private static string ResolveLinks(string body)
{
   body = System.Text.RegularExpressions.Regex.Replace(body, "(http://[^ ]*[a-zA-Z0-9])", "<a href=\"$1\" target=\"_blank\">$1</a>");
   body = System.Text.RegularExpressions.Regex.Replace(body, "(www.[^ ]*[a-zA-Z0-9])", "<a href=\"http://$1\" target=\"_blank\">$1</a>");
   return body;
}

All the comments in this application passes through this method, and all typed URLs get transformed.