HttpHandlers in ASP.NET without regions

Jul 12, 2006

 

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.

* Only $4.95/month ASP.NET & Windows 2008 + IIS 7 Hosting! FREE SQL Included

Comments (3) -

 William Forney
William Forney
7/24/2006 1:22:51 PM #

You can put the .ashx code into a code-behind file (it takes a little tinkering, but you can do it). Visual Studio does not directly do this for you but once it is done it is smart enough to know what you did. Anyway, once the code is in a .cs file the regions work.

 Mads Kristensen
Mads Kristensen
7/24/2006 4:56:21 PM #

William. You are absolutely right and I also use that approach a lot when I'm doing class libraries or for any other kind of easy distribution. However, some times it seems right to use .ashx files and that's where the problem occurs.

 Ihar Bury
Ihar Bury
7/25/2006 4:31:09 PM #

I don't think it's a good idea to put much code into an *.ashx file. It's generally going to be just a method call with possibly an object creation. It's better to place handling logic into separate class or even to divide it into some classes.

Comments are closed

About the author

Mads Kristensen

Mads Kristensen
Program Manager at the Microsoft Web Platform team and founder of BlogEngine.NET.

More...

Month List

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer’s view in any way.