简体   繁体   中英

How to configure an Http Handler in IIS 7?

Here's what I want to do:

  1. I've created a class library project and this has a class implementing the IHttpHandler interface. Let's call this class ZipHandler. Let's say the namespace is Zip.
  2. I want that whenever any Http request comes for a zip file, my ZipHandler should handle it, regardless of whether the request is to an Asp.Net application or a normal virtual directory.

Queries:

  1. Is it possible (it should be given the hype about integrated pipeline etc. in IIS 7)?
  2. How to do it?

Here's the info I was looking for:

If you want to register your custom HTTP handler at the IIS 7 Web server level, you must compile your HTTP handler into a strongly-named assembly and deploy it to the Global Assembly Cache (GAC) because IIS 7 only picks up assemblies deployed to the GAC. It does not pick up assemblies deployed anywhere else such as the bin directory of a particular Web site or Web application.

We're aiming to add this handler at web server level. After deploying the handler in GAC, open the web.config available at the web server level (right click and browse -> open the web.config show here) and put something like this in the handler section (the fully qualified name of the class):

<handlers>
<add name="Ch8_RssHandler" path="*.rss" verb="*"
type="ProIIS7AspNetIntegProgCh8.RssHandler, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=369d834a77" preCondition="integratedMode" />
</handlers>

Note: The information snippets (1st para and code sample) are taken from the book:
Professional IIS 7 and ASP.Net Integrated Programming by Dr. Shahram Khosravi

Seems like a very nice book :)

This MSDN article How to: Configure an HTTP Handler Extension in IIS explains what you'll have to do. See the paragraph for the Integrated mode.

The file-name extension for .zeip must be registered in both the httpHandlers element and the handlers element.

You'll have to click Add Managed Handler in the Actions pane.

Using IIS Manager in IIS 7.0 to add a custom handler extension is equivalent to registering the handler extension in the Web.config file.

I did a test in VS2012

My handler is like this

namespace MyProject
{
    public class ZipHandler: IHttpHandler
    {
        public bool IsReusable { get { return true; } }
        public void ProcessRequest(HttpContext context) { ... }
    }
}

My web.config is

<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.webServer>
    <handlers>
      <add
        name="ZipHandler"
        path="*.zip"
        verb="*"
        type="MyProject.ZipHandler"
        preCondition="integratedMode"
      />
    </handlers>
  </system.webServer>

</configuration>

In this way I can ask for "foo.zip" and have my handler get the request.

There's also a post from Rick Strahl that can help you troubleshoot things about handlers and modules: HttpModule and HttpHandler sections in IIS 7 web.config files

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM