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 t开发者_Python百科hat 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!
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" />
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
精彩评论