Okay, some background information: I was sick of cutting hundreds of images out of photoshop files for headings, so I created a handler (.ashx) that generated the headings on-the-fly (with caching of course). That was working nicely, but what I was starting to get frustrated about was the need to include the file in the structure of the website (it just didn't seem right). So, I came up with an alternate solution. Here's what needed to be done:
Step 1
Create a new class that inherits from IHttpHandler. In my example, I named the class DynamicImageHandler and placed it in the namespace DrewWalker.Web.Handlers. The logic behind the naming was to follow Microsoft's conventions for System.Web.Handlers.TraceHandler, as I was aiming to echo the functionality of Trace.axd (you'll see what I mean).
Step 2
Move the code from the .ashx to the new class (straightforward).
Step 3
Register the handler in the httpHandlers configuration section of the web.config. Here is an example:
<configuration>
<system.web>
<httphandlers>
<add type="DrewWalker.Web.Handlers.DynamicImageHandler" path="DynamicImage.axd" verb="*">
</httphandlers>
</system.web>
</configuration>
Voila! Once you compile you will be able to reference your handler as though it were a file sitting in the structure of your website. So in my case I can now browse to http://localhost/DynamicImage.axd?Text=Hello%20World and see a nice jpeg with Hello World.
0 comments:
Post a Comment