views:

69

answers:

1

I have a requirement on my new project to serve up some "hidden" assets (actually just stashed in the App_Data directory) after a certain date. Before then, they should act like they aren't there.

I've done this kind of thing a hundred times with Page object, but as I started work on this, I thought I'd look into handlers. Having never worked with them (and being a little intimidated by them), I was happy to find that they'd serve up my XML and JPG files without the overhead of the whole Page class. Already, I'm happy that I considered it. I wrote it to handle functionality like "MyHandler.ashx?secretfile=blah.xml", and it worked great.

Then I started looking at special extension handling, so that a request for "blah.xml.secret" would get picked up by handler and return blah.xml after checking the date. A couple of lights went off in my head, and I reworked the code so that it handled that case. It worked (in the IDE)! I was pretty excited.

Getting it onto the dev server (IIS) was a little different: I had to register .secret as a .NET type (no big deal), and it still didn't work until I unchecked the "verify file exists" checkbox. (blah.xml.secret obviously doesn't exist: blah.xml does, but not in the spot it's being asked for, only in the secured App_Data directory.) That's not a huge deal, but now my clever solution relies on two implementation details from the IIS side.

So my question is: is this the intended use of handlers in asp.net? Am I warping this beyond recognition? I feel like I've seen sites do tricks like this in the past, but for the one thing I'm trying to do, the IIS changes seem overly complicated. In my research on this, I didn't find a slam dunk 1-2-3 guide to using handlers that included an example like this, so it's got me thinking I'm maybe abusing it or going about it the wrong way.

+2  A: 

Yes, that's pretty much how it works. (In Windows Server 2008 there is a remote chance that you can do the settings from web.config so that you don't have to change anything in IIS.)

If you use an extension that isn't already registered to be handled by the ASP.NET engine, you have to register it. If you use an extension that already is handled by ASP.NET, like .aspx, then you don't have to register anything in IIS. (When you run it in the integrated web server in Visual Studio, everything is already handled by ASP.NET, that's why it works there.)

Guffa