简体   繁体   中英

Custom HttpHandler not firing, returning 404 in ASP.NET MVC Application

I don't know if it is relevant that this is happening in an MVC website but thought I'd mention it anyway.

In my web.config I have these lines:

<add verb="*" path="*.imu" type="Website.Handlers.ImageHandler, Website, Version=1.0.0.0, Culture=neutral" />

in the Website project I have a folder named Handlers which contains my ImageHandler class. It looks like this (I have removed the processrequest code)

using System;
using System.Globalization;
using System.IO;
using System.Web;

namespace Website.Handlers
{
    public class ImageHandler : IHttpHandler
    {
        public virtual void ProcessRequest(HttpContext context)
        {
            //the code here never gets fired
        }

        public virtual bool IsReusable
        {
            get { return true; }
        }
    }
}

If I run my website and go to /something.imu it just returns a 404 error.

I am using Visual Studio 2008 and trying to run this on the ASP.Net development server.

I've been looking for several hours and had it working in a seperate empty website. So I don't understand why it won't work inside an existing website. There are no other references to the *.imu path btw.

I suspect that this has everything to do with the fact that you are using MVC, as basically it takes control of all incoming requests.

I suspect you will have to use the routing table, and possibly create a new routing handler. I haven't done this myself but something like this might work:

void Application_Start(object sender, EventArgs e) 
{
    RegisterRoutes(RouteTable.Routes);
}

public static void RegisterRoutes(RouteCollection routes)
{
    routes.Add(new Route
    (
         "{action}.imu"
         , new ImageRouteHandler()
    ));
}

and then the ImageRouteHandler class will then return your custom ImageHttpHandler , although from looking at examples on the web it might be better to change that so it implements MvcHandler , rather than straight IHttpHandler .

Edit 1: As per Peter's comment, you can also ignore the extension by using the IgnoreRoute method:

public static void RegisterRoutes(RouteCollection routes)
{
  routes.IgnoreRoute("{resource}.imu/{*pathInfo}");
}

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