views:

248

answers:

1

Can anybody please help me with this as I have no idea why public IHttpHandler GetHttpHandler(RequestContext requestContext) is not executing. In my Global.asax.cs I have

public class MvcApplication : System.Web.HttpApplication
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            "Default",                                              // Route name
            "{controller}/{action}/{id}",                           // URL with parameters
            new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
        );

        routes.Add("ImageRoutes", new Route("Images/{filename}", new CustomRouteHandler()));

    }

    protected void Application_Start()
    {
        RegisterRoutes(RouteTable.Routes);
    }
}

//CustomRouteHandler implementation is below

public class CustomRouteHandler : IRouteHandler
{
    public IHttpHandler GetHttpHandler(RequestContext requestContext)
    {
        // IF I SET A BREAK POINT HERE IT DOES NOT HIT FOR SOME REASON.  
        string filename = requestContext.RouteData.Values["filename"] as string;

        if (string.IsNullOrEmpty(filename))
        {
            // return a 404 HttpHandler here 
        }
        else
        {
            requestContext.HttpContext.Response.Clear();
            requestContext.HttpContext.Response.ContentType = GetContentType(requestContext.HttpContext.Request.Url.ToString());

            // find physical path to image here.   
            string filepath = requestContext.HttpContext.Server.MapPath("~/logo.jpg");

            requestContext.HttpContext.Response.WriteFile(filepath);
            requestContext.HttpContext.Response.End();

        }
        return null;
    }
}

Can any body tell me what I'm missing here. Simply public IHttpHandler GetHttpHandler(RequestContext requestContext) does not fire.

I havn't change anything in the web.config either. What I'm missing here? Please help.

A: 
routes.MapRoute(
    "Default",                                              // Route name
    "{controller}/{action}/{id}",                           // URL with parameters
    new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
);

routes.Add("ImageRoutes", new Route("Images/{filename}", new CustomRouteHandler()));

You need to flip these two declarations. {controller}/{action}/{id} is probably matching the incoming URL, so you need to declare your special Images/{filename} before it.

Levi
Thanks. But still doesn't work.
Peter
Sorry your solution is part of the problem, but I found more into this:) It is my fault. I had my images under a directory called Content i.e Content/Images/{filename}. Should have double checked before submitting the post. Apologies, and thanks heaps for your help.
Peter