29

I have a HttpHandler that resizes images based on the querystring, so requesting something like:

http://server/image.jpg?width=320&height=240

will give you a resized image that's 320x240.

In the IIS Manager, under Handler Mappings, I mapped my handler's path as *.jpg,*.gif,*.bmp,*.png. However, this doesn't activate the handler. If I change it to just *.jpg, then it works.

My question is, do I have to create 4 separate mapping entries, one for each image type, or is there some way to combine multiple extensions in one path?

Oleg Grishko
  • 4,132
  • 2
  • 38
  • 52
Daniel T.
  • 37,212
  • 36
  • 139
  • 206

2 Answers2

20

Daniel T's answer:

Turns out that IIS 7's handler mapping is different than IIS 6's handler mapping. In IIS 6, you can map your handlers like this in web.config:

<configuration>
  <system.web>
    <httpHandlers>
      <add verb="GET" path="*.jpg,*.gif,*.bmp,*.png" type="YourProject.ImageHandler" />
    </httpHandlers>
  </system.web>
</configuration>

It allows you to use multiple paths, comma-delimited. In IIS 7, it's in a different section:

<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>

It doesn't support multiple paths, so you need to map your handler for each path.

You'll probably have to end up mapping it in both places because Visual Studio's internal dev server uses IIS 6 (or IIS 7 running in compatibility mode), whereas the production server is probably using IIS 7.

Jay Sullivan
  • 17,332
  • 11
  • 62
  • 86
Oleg Grishko
  • 4,132
  • 2
  • 38
  • 52
  • 2
    This was the answer for me, I upgraded from 3.5 & MVC 2 to 4.5.2 & MVC 3. I knew to move the to the section, but until now I didn't realise that the usage of 'path' had to be altered, thanks! – WillDud Nov 04 '15 at 11:37
  • After migrating a 5-year-old project to VS 2017 and trying to run in IISExpress, I was getting 404's on files that the tracer confirmed existed. It wasn't permissions, or paths; it was lack of handlers and your answer clarified the 2 approaches in a way that finally helped me see this. I suspect that in the past, handlers had been added to the server rather than the site/project. – rainabba Jun 28 '19 at 18:25
6

You can add multiples of the same handler so long as you change the name attribute.

Krisc
  • 1,357
  • 2
  • 14
  • 22