Howto: create a custom httpHandler
By rickvdbosch
- 2 minutes read - 367 wordsIf you would like to (for example) make sure all the jpegs on your site are displayed including a copyright text or something like that, you could use the example I posted before. But it would be easier to write a custom httpHandler. Here’s a small howto:
-
Add a class to a WebApplication, and give it a decent name. Mine is customHandler.
-
Have the class implement the IHttpHandler interface (found in the System.Web namespace)
-
Implement the right methods
-
Use ‘ProcessRequest’ to process the request the way you want to
-
Register the handler in the web.config
-
‘Register’ the handler in IIS
-
You’re good to go!
Now for some exampe-code. Steps 1 to 4 can be found here:
public class customHandler : IHttpHandler { public customHandler() { }
<p>
context.Response.ContentType = "image/jpeg";<br /> image.Save(context.Response.OutputStream, ImageFormat.Jpeg);<br /> context.Response.End();<br /> </font><font size="2">}</p>
<p>
</font><font color="#0000ff" size="2"> public</font><font size="2"> </font><font color="#0000ff" size="2">bool</font><font size="2"> IsReusable<br /> {<br /> </font><font color="#0000ff" size="2"> get<br /> </font><font size="2"> {<br /> </font><font color="#0000ff" size="2"> return</font><font size="2"> </font><font color="#0000ff" size="2">true</font><font size="2">;<br /> }<br /> }<br /> }</p>
<p>
Step 5 looks a little bit like this:
</p>
<p>
<font color="#0000ff" size="2"><</font><font color="#800000" size="2">httpHandlers</font><font color="#0000ff" size="2">><br /> </font><font color="#0000ff" size="2"> <</font><font color="#800000" size="2">add</font><font color="#ff00ff" size="2"> </font><font color="#ff0000" size="2">verb</font><font color="#0000ff" size="2">="*"</font><font color="#ff00ff" size="2"> </font><font color="#ff0000" size="2">path</font><font color="#0000ff" size="2">="*.jpg"</font><font color="#ff00ff" size="2"> </font><font color="#ff0000" size="2">type</font><font color="#0000ff" size="2">="handlerTest.customHandler, handlerTest"/><br /> </font><font color="#0000ff" size="2"></</font><font color="#800000" size="2">httpHandlers</font><font color="#0000ff" size="2">></p>
<p>
<font color="#000000">Step 6 is a little bit tricky. You will have to open the Internet Information Services manager and open the properties for the the website you want the handler to work for. Om the first tab page, open up configuration. Now, first select a file extension which is served by ASP.NET, like .asax or .aspx and click edit. Copy the file that's used over there (it should be C:WINDOWSMicrosoft.NETFrameworkv1.1.4322aspnet_isapi.dll). Close the dialog and click to add a new one. Here, put the copied value in the right textbox and fill in .jpg as the extension you want to be handled. Next close all open boxes by clicking OK and you should be ready to go.</p>
<p>
The image it produced with my code:<br /> <img height="300" src="/images/posts/sites/9/2014/01/handlertest.jpg" width="199" alt="" /></font></font></font>
</p>