views:

148

answers:

2

I have an MVC2 application, and HttpHandler Library. The library, to simplify, serves an image. The problem is that if I invoke this handler from the root page ( http://whatever/ ), everything works fine. If, however, I change the Global.asax, so the same page now has the address of http://whatever/controller/action - it doesn't work (ALT text comes out instead of image).

I didn't write the handler library, but I have the code; so I put a breakpoint in ProcessRequest function of IHttpHandler implementation. For good situation the breakpoint gets triggered, but for bad case it isn't. So, I assume that the handler isn't invoked for non-root pages.

This is what I have in system.webServer/handlers section:

<add name="ImageHandler" verb="*" 
              path="ImageLib.axd" type="ImageHandler, ImageHandler" />

The view page has

<img src="ImageLib.axd?image=img001.jpg" alt="real image here" />

I don't know if MVC plays any role in it... the old WebForms application works fine with the same settings.

Hopefully, I provided all that I know. Of course, if something isn't clear, I'll be happy to explain as much as I can!

+1  A: 

Try this:

<img src="/ImageLib.axd?image=img001.jpg" alt="real image here" />

The fundamental problem is that you are relatively referencing ImageLib.axd. A browser will try to access it from wherever it is...in your case, when it requests the image, it is requesting it from http://whatever/controller/action/ImageLib.axd. I am assuming you need it to request from http://whatever/ImageLib.axd.

Note that simply adding the preceeding whack (/) is not necessarily going to solve your problem. If ImageLib.axd is in some sub folder off of your root, you would have to include that. If your ImageLib.axd handler is in a folder called handlers off of your web site root, you would do the following:

<img src="/Handlers/ImageLib.axd?image=img001.jpg" alt="real image here" />
jrista
OK, simply adding preceding slash *did* work. I am not sure about the second part - I don't have physical Image.axd. I think that Image.axd is mapped to ImageHandler, that is included in ImageUtil.dll that is included as reference. I guess, it is implied to be in the root; therefore slash solves the problem.Thank you very much!
Felix
Ah, yeah, not all .axd handlers have an actual .axd file...kind of depends on the implementation. Glad the solution worked. :)
jrista
A: 

Hi Felix,

You need to route the request for images to your httpHandler, but mvc is is intercepting the requests and trying to route to a controller action.

You can configure this in your Global.asax or if you're implementing your own HttpApplication on the OnStart() method as follow:

routes.IgnoreRoute("{*allaxd}", new {allaxd=@".*\.axd(/.*)?"});
// or make sure you have this 
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

You can find more info on a post by Phil Haack here http://haacked.com/archive/2008/07/14/make-routing-ignore-requests-for-a-file-extension.aspx

Dax70
Dax70, thank you for your suggestion. I have this line (I guess, it's put in RegisterRoutes() when MVC project is generated). Also, I am not sure how this suggestion applies to my situation - since I *do* get the httphandler invoked for root pages.
Felix