如何在IIS7中为HttpHandler注册多个路径?

Dan*_* T. 28 asp.net iis iis-7 httphandler

我有一个HttpHandler,它根据查询字符串调整图像大小,所以请求类似于:

HTTP://server/image.jpg宽度= 320&高度= 240

将为您提供320x240的重新调整大小的图像.

IIS Manager下面Handler Mappings,我将处理程序的路径映射为*.jpg,*.gif,*.bmp,*.png.但是,这不会激活处理程序.如果我把它改成只是*.jpg,那就行了.

我的问题是,我是否必须创建4个单独的映射条目,每个映像类型一个,或者是否有某种方法可以在一个路径中组合多个扩展?

Ole*_*hko 18

Daniel T的回答是:

事实证明,IIS 7的处理程序映射与IIS 6的处理程序映射不同.在IIS 6中,您可以在以下位置映射处理程序web.config:

<configuration>
  <system.web>
    <httpHandlers>
      <add verb="GET" path="*.jpg,*.gif,*.bmp,*.png" type="YourProject.ImageHandler" />
    </httpHandlers>
  </system.web>
</configuration>
Run Code Online (Sandbox Code Playgroud)

它允许您使用逗号分隔的多个路径.在IIS 7中,它位于不同的部分:

<configuration>
  <system.webServer>
    <handlers>
      <add name="ImageHandler for JPG" path="*.jpg" verb="GET" type="YourProject.ImageHandler" resourceType="File" />
      <add name="ImageHandler for GIF" path="*.gif" verb="GET" type="YourProject.ImageHandler" resourceType="File" />
      <add name="ImageHandler for BMP" path="*.bmp" verb="GET" type="YourProject.ImageHandler" resourceType="File" />
      <add name="ImageHandler for PNG" path="*.png" verb="GET" type="YourProject.ImageHandler" resourceType="File" />
    </handlers>
  </system.webServer>
</configuration>
Run Code Online (Sandbox Code Playgroud)

它不支持多个路径,因此您需要为每个路径映射处理程序.

您可能不得不最终在两个地方映射它,因为Visual Studio的内部开发服务器使用IIS 6(或在兼容模式下运行的IIS 7),而生产服务器可能使用IIS 7.

  • 这是我的答案,我从3.5和MVC 2升级到4.5.2和MVC 3.我知道将<httpHandlers>移动到<handlers>部分,但直到现在我还没有意识到'使用'路径'必须改变,谢谢! (2认同)

Kri*_*isc 6

只要更改name属性,就可以添加同一处理程序的多个.