简体   繁体   中英

IRouteHandler not routing through RouteConfig

I have a IRouteHander class which I use to resize images on the fly and add expire headers to them, Recently I moved to MVC5 and now updating my code. I tried to register the same route for that class in RouteConfig.cs

routes.Add(new Route("Image/{w}/{h}/{src}", new ThumbImageRouteHandler()));

but this route isn't working anymore like it was on MVC3 and giving 404 error in MVC5. Is there anything I am missing here? this route leads to

public class ThumbImageRouteHandler : IRouteHandler
{
         public IHttpHandler GetHttpHandler(RequestContext requestContext)
            {
                HttpHanler httpHandler = new HttpHanler();
                return httpHandler;
            }
            public class HttpHanler : IHttpHandler
            {
                public bool IsReusable
                {
                    get
                    {
                        return false;
                    }
                }
                public void ProcessRequest(HttpContext context)
                {
                //Do something
                }
               }
           }
}

Please help me fixing this issue. Thanks

After research I found out that I need to add a line in webconfig in order to make it work, here's how.

  <system.webServer>
    <handlers>
      <add name="ApiURIs-ISAPI-Integrated-4.0-Image" path="/Image/*" verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
    </handlers>
  </system.webServer>

Because IRouteHandler is generating re-sized images with a dynamic path, and IIS thinks this is the actual path to a directory because of dot(.) in the link and it thinks it's an extension, which is actually not. So we have to add a handler in Web.Config to make it work.

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