Infosys Microsoft Alliance and Solutions blog

« SQL Server 2005 SP 2 Released | Main | Microsoft Vista: Best Practices recommendation for Zero Touch Deployment using SMS and BDD-II »

Configuring Custom Http Handler in IIS 7

Recently when working on an application, I was playing around with writing custom http handlers. There is nothing new in that lots has been written about how to build and configure custom http handlers. So when after writing this I decided to configure it in IIS 7 on my Vista machine, I was pleasently surprised to see that I was lost !

IIS 7 UI has changed noticeably from its earlier versions and when I wanted to configure the file extension I wanted my custom handler to work with, I didn't know what to do.

Fortunately, I found that when viewing my particular website in IIS 7, in the two groups that IIS displayed the information (Grouped by Area by default and has ASP.NET and IIS groups), there was an option called Handler Mappings in IIS group. This seemed like the most relevent location where to look further.

Opening the Handler Mappings, the listing for all file extensions registered with various handlers was displayed, so this was the right place. However as I looked for an "Add new file mapping" option, I instead found three options

1. Add Managed Handler...
2. Add Script Map...
3. Add Module Mapping...

Nothing what each of these meant, I clickly opened each and figured out that managed handler had something to do with handlers witten in managed code, script map was to help configuring an executable and module mapping to work with http Modules. For more details do refer to the IIS 7.0 help file installed with IIS or online on technet.

I soon realized that Add Managed Handler is what I need to work with. There is a catch about Application pool setting being Integrated or ISAPI, which again you can get more details about in the IIS 7 help files. I did notice that on my Vista RTM, the two modes were Integrated and Classic. Since I didn't work with pre RTM versions, I can only guess that Classic was earlier called as ISAPI.

OK, so back to adding managed handlers. See the figure below to see how this looks like.

ManagedHandler.jpg

The interesting point is that after adding a custom http handler (you can follow MSDN documentation's How To article for this) and building the site, IIS already seemed to understand that I had a custom Http handler in my application and it displayed it in the Type drop down (HelloWorldHandler in the figure above). I selected it, provided *.sample as the extension I wanted to work with and also gave it a friendly name.
 
Completing the above steps also automatically edited the web.config file to add the necessary information about the custom handler. However unlike earlier where we added <httpHandlers> within <configuration><system.web> node, I got a new entry in the web.config inside of <configuration> node as below

<system.webServer>
    <handlers>
        <add name="Custom Handler" path="*.sample" verb="*" type="HelloWorldHandler"
                     resourceType="Unspecified" />
    </handlers>
</system.webServer>

Finally to test that it all worked, I accessed the handler as http://localhost/MySampleSite/Test.sample and I did see the message displayed on the page as written on the httpHandler code.

Hence when working with IIS 7, one needs to write the implementation class for the http handler and build the project. Configuring in IIS and also making the necessary entries in Web.config is easily taken care via the Add Managed Handler option in IIS 7.

Comments

How can i make that custom extension to be handled by aspnet_isapi.dll? So, for example a file named test.sample should be treated as if it were test.aspx?? I have been playing around on IIS 7 on Windows Vista Home Premium but no luck.

Rafay, you will find some information here - http://blogs.iis.net/thomad/archive/2006/11/04/precondition-what.aspx

It was really helpful.

I was changing a Web Site from a Windows Server 2003 (IIS 6) to a Web Server 2008 (IIS 7) and there were lots of Crystal Reports with logos and fixed images that weren't displayed! (even though the web.config had the http handler for the Crystal Report Image Handler configured!)

Your post gave me a clue. Thank you very much!

el-james, glad this helped.

Can you specify a folder in the path? Eg, I want to handle all pdf files in a particular folder, but not any other...

Vaishali, it has been a long time since I worked on this and I don't have the bandwidth to check on this. A few suggestions to check on

in the configuration file in the path, you can try giving the folder name and then the file extension. For eg - path="folder/*.pdf"

Thanks for the post! I ran into this issue after migrating from IIS6 to IIS7 where I was using an HttpHandler that needed to be added to system.WebServer.

Verify that the Web service extension requested is enabled on the server.

1. Open the IIS Manager and navigate to the server level.
2. In the Features view, double-click ISAPI and CGI Restrictions to verify that the Web service extension is set to Allowed.
3. If the extension is not in the list, click Add in the Actions pane.
4. In the Add ISAPI and CGI Restrictions dialog box, type the path of the .dll or .exe file in the ISAPI or CGI Path box, or click Browse to navigate to the location of the file.
5. In the Description box, type a brief description of the restriction.
6. (Optional) Check "Allow extension path to execute" to allow the restriction to run automatically. If you do not check this option, the restriction status is Not Allowed, which is the default. You can allow the restriction later by selecting it and clicking Allow on the Actions pane.
7. Click OK.

I am working using custom IHttpHandler in my application. It is working fine in IIS 5. But now we are migrating to IIS 7 so we bought 2008 R2 server. Here it is not working. Kindly Help. you can reach me in maheswaran@lucidindia.com. Thanks in advance. This is my Custom Handler Code :

public class WebRequestHandler : IHttpHandler, IRequiresSessionState
{
private void ProcessDataRequest(HttpContext context)
{
Controller controller = new Controller();
controller.ProcessDataRequest(context);
}


region IHttpHandler Members

public bool IsReusable
{
get { return true; }
}


public void ProcessRequest(HttpContext context)
{
var segments = context.Request.Url.Segments;
var request = segments[segments.Length - 1];

context.Response.Expires = -1;

switch (request)
{
case "data.lst":
this.ProcessDataRequest(context);
return;
}
}


endregion
}


And i am calling like this.

this.client = new WebClient();
this.client.DownloadProgressChanged += this.OnProgressChanged;
this.client.OpenReadCompleted += this.OnDataAvailable;
this.client.OpenReadAsync(new Uri("../data.lst?viewerID=viewer", UriKind.Relative), "GET");
But the ProcessRequest(HttpContext context) method is not called in IIS 7 after publishing.
Kindly Reply to this post. Thanks in advance.
This is my web.config content: configuration>
appSettings>
add key="ChartHttpHandler" value="Storage=memory;Timeout=180;Url=~/temp/;"/>
connectionStrings/>
system.web>
profile>
properties>
add name="searchSettings" defaultValue="" type="System.String"/>
/properties>
/profile>
sessionState timeout="2">
/sessionState>
compilation debug="true" targetFramework="4.0">
assemblies>
add assembly="System.Windows.Forms, Version=4.0.0.0, Culture=neutral,
PublicKeyToken=B77A5C561934E089"/>
authentication mode="Windows"/>

httpHandlers>
add verb="GET,POST" path="*.lst" type="App_Code.WebRequestHandler"/>
/httpHandlers>
pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID"/>
system.webServer>
validation validateIntegratedModeConfiguration="false"/>
handlers>
add name="WebRequests" verb="" path=".lst" modules="IsapiModule"
scriptProcessor="%path%\aspnet_isapi.dll" ype="App_Code.WebRequestHandler"/> /handlers>
/system.webServer>
system.serviceModel>
behaviors>
serviceBehaviors>
/serviceBehaviors>
/behaviors>
services>
/services>
/system.serviceModel>
/configuration>

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)

Please key in the two words you see in the box to validate your identity as an authentic user and reduce spam.

Subscribe to this blog's feed

Follow us on

Blogger Profiles

Infosys on Twitter